python 中的casefold()
函數通過移除字符串中存在的所有區分大小寫來幫助將字符串轉換為小寫。這類似于casefold()
方法,但casefold()
更強大、更具攻擊性。
**string.casefold()**
casefold()
參數:
casefold()
方法不接受任何參數。這個方法可以在比較兩個字符串時找到更多的匹配,并且可以將更多的字符轉換為小寫。
casefold()
返回值
返回值始終是字符串。此方法在字符串比較時忽略大小寫。
| 投入 | 返回值 | | 線 | casefolder 字符串 |
Python 中casefold()
方法的示例
示例 1:使用casefold()
的 Python 小寫轉換
string = "HOW ARE YOU"
# print lowercase string
print("After lowercase conversion:", string.casefold())
輸出:
After lowercase conversion: how are you
示例 2:使用casefold()
嚴格轉換
string1 = "der Flu?"
string2 = "der Fluss"
# ? is equivalent to ss
if string1.casefold() == string2.casefold():
print('The given strings are equal.')
else:
print('The given strings are not equal.')
輸出:
The given strings are equal.
注意:德語小寫字母已經是小寫了,casefold()
將其轉換為 ss,如果使用lower()
方法,它什么也不做。