12 Python 中 字串大小寫轉換

2021-08-10 17:48:44 字數 1010 閱讀 9736

簡單總結下python中字串大小寫轉換,最後有個處理列表的方法感覺有點意思

a = 

'hello python'#都是小寫

b =

'hello python'#第乙個字母大寫

c =

'hello python'#每個單詞的第乙個字母大寫

d =

'hello python'#所有字母都是大寫

f =

'hello,python'#用逗號分隔

e = ['hello','python'] #列表

print(a.capitalize()) #首字母大寫

print(d.lower()) #全部小寫

print(a.upper()) #全部大寫

print(a.title()) #每個單詞第乙個字母大寫

print(b.swapcase()) #大小寫切換

print(f.title()) #用逗號分隔,仍然會將逗號後面的看成乙個新單詞

x = [item.lower() for

item in

e] #將乙個列表中的元素依次處理

print(x)

執行結果:

c:\python36\python.exe e:/python/test1/linshi/8.py

hello python

hello python

hello python

hello python

hello python

hello,python

['hello', 'python']

process finished with exit code 0

python 中字串大小寫轉換

python中字串的大小寫轉換和判斷字串大小寫的函式小結 一 pyhton字串的大小寫轉換,常用的有以下幾種方法 1 對字串中所有字元 僅對字母有效 的大小寫轉換,有兩個方法 print just to test it upper 所有字母都轉換成大寫 just to test it print j...

Python 字串大小寫轉換

filename test.py author by www.runoob.com str www.runoob.com print str.upper 把所有字元中的小寫字母轉換成大寫字母 print str.lower 把所有字元中的大寫字母轉換成小寫字母 print str.capitaliz...

Python 字串大小寫轉換

字串大小寫轉換 1 upper 字串中所有字元轉成大寫。2 lower 字串中所有字元轉成小寫。3 swapcase 字串中所有字元,大寫轉成小寫,小寫轉成大寫。4 capitalize 第乙個字元轉成大寫,其餘字元轉成小寫。5 title 每個單詞第乙個字元轉成大寫,其餘字元轉成小寫。1 字串的大...