Python 字串大小寫轉換

2022-09-24 04:27:11 字數 666 閱讀 2000

字串大小寫轉換

1、upper()

字串中所有字元轉成大寫。

2、lower()

字串中所有字元轉成小寫。

3、swapcase()

字串中所有字元,大寫轉成小寫,小寫轉成大寫。

4、capitalize()

第乙個字元轉成大寫,其餘字元轉成小寫。

5、title()

每個單詞第乙個字元轉成大寫,其餘字元轉成小寫。

1

#字串的大小寫轉換

2 s = '

hello,python

'3 a = s.upper() #

轉成大寫後會產生新的字串

4print

(s, id(s))

5print

(a, id(a))

67 b = s.lower() #

轉成小寫後會產生新的字串

8print

(s, id(s))

9print

(b, id(b))

1011 s2 = '

hello,python'12

print

(s2.swapcase())

1314

print(s2.title())

Python 字串大小寫轉換

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

大小寫轉換 字串

time limit 1000ms memory limit 65536kb problem description 把乙個字串裡所有的大寫字母換成小寫字母,小寫字母換成大寫字母。其他字元保持不變。input 輸入為一行字串,其中不含空格。長度不超過80個字元。output 輸出轉換好的字串。exa...

Python 字串字母大小寫轉換

1 str.lower 返回小寫字串 對原字串沒有改變,而是直接返回乙個新的字串 s zxf s1 s.lower print s zxf print s1 zxf 2 str.upper 返回大寫字串 s zxf print s.upper zxf 3 str.capitalize 將首字母變大寫...