Python中 capitalize() 函數(shù)的作用是把一個字符串的首字符變?yōu)榇髮懀溆嘧址優(yōu)樾懀疚囊榻B的 casefold() 函數(shù)是把字符串中的所有字母都變成小寫的形式。
且該函數(shù)僅對字符串對象起作用,而對非字符串對象無效。
該函數(shù)也會把非英文字母的大寫形式變成其對應(yīng)的小寫形式
該函數(shù)的語法格式如下:
string_obj.casefold()
該函數(shù)執(zhí)行完畢后,將返回一個新的字符串。
str1 = "Hello,WWW.Biye5u.COM"
str2 = str1.casefold()
print(str2)
輸出:hello,www.survivalescaperooms.com
從結(jié)果中可以看出,該函數(shù)將字符串中的所有英文字母都變成了小寫形式。
也可以像下面這種使用該函數(shù):
s = "Hello,WWW.Biye5u.COM".casefold()
print(s)
輸出:hello,www.survivalescaperooms.com
str1 = "Σ'αγΑΠ?." #意思是:我愛你.
str2 = str1.casefold()
print(str2)
輸出:σ'αγαπ?.
從這里看出,Python中的 casefold() 函數(shù)對于非英文字母同樣可以實現(xiàn)大小寫轉(zhuǎn)換。
str1 = "武林網(wǎng)VEVB的網(wǎng)址是:WWW.Biye5u.COM"
str2 = str1.casefold()
print(str2)
輸出:武林網(wǎng)it樂園的網(wǎng)址是:www.survivalescaperooms.com
從這里可以看出,casefold()函數(shù)只將字母轉(zhuǎn)換為小寫,其余字符原樣輸出。
str1 = "我是一個流浪的程序員"
str2 = str1.casefold()
print(str2)
輸出:我是一個流浪的程序員
str1 = ""
str2 = str1.casefold()
print(str2)
該程序可以正確執(zhí)行,輸出空行
s1 = None
print(s1.casefold()) #會發(fā)生錯誤
s2 = 12.34
print(s2.casefold()) #會產(chǎn)生錯誤
上面兩個例子,python在執(zhí)行時會報錯,如下圖所示:
即casefold()函數(shù)不能使用于非字符串對象。
新聞熱點
疑難解答
圖片精選