Python casefold()

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()方法,它什么也不做。

你可能感兴趣的