python upper()函数和lower()函数(返回字符串中字母的大[小]写)(大写、小写)
生活随笔
收集整理的這篇文章主要介紹了
python upper()函数和lower()函数(返回字符串中字母的大[小]写)(大写、小写)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
upper()方法
Python upper() 方法將字符串中的小寫字母轉為大寫字母。
str.upper()
#!/usr/bin/python3str = "this is string example from runoob....wow!!!";print ("str.upper() : ", str.upper())結果:
str.upper() : THIS IS STRING EXAMPLE FROM RUNOOB....WOW!!!lower()方法
Python lower() 方法轉換字符串中所有大寫字符為小寫。
str.lower()
#!/usr/bin/python3str = "Runoob EXAMPLE....WOW!!!"print( str.lower() )結果:
runoob example....wow!!!示例1
做個練習,利用map()函數,把用戶輸入的不規范的英文名字,變為首字母大寫,其他小寫的規范名字。輸入:[‘adam’, ‘LISA’, ‘barT’],輸出:[‘Adam’, ‘Lisa’, ‘Bart’]:
""" 第一個版本 """ # -*- coding: utf-8 -*-def normalize(name):return name.upper()[0:1] + name.lower()[1:]L1 = ['adam', 'LISA', 'barT'] L2 = list(map(normalize, L1)) print(L2)""" 寫完第一個版本后,雖然完成了需求,但是其實有可以優化的地方 覺得下面的寫法更pythonic些 """ # -*- coding: utf-8 -*-def normalize(name):this_name = name.lower()return this_name[0:1].upper() + this_name[1:].lower()L1 = ['adam', 'LISA', 'barT'] L2 = list(map(normalize, L1)) print(L2)參考文章1:【菜鳥教程】Python3 lower()方法
參考文章2:一篇文章搞懂Python中的函數式編程
總結
以上是生活随笔為你收集整理的python upper()函数和lower()函数(返回字符串中字母的大[小]写)(大写、小写)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 函数式编程是啥玩意?map() redu
- 下一篇: python filter()函数(与m