Python String casefold() Method

The Python casefold() function works as a stronger version of Python’s lower() function, i.e. it has the same functionality of returning a lowercased copy of the given string, but it takes the method one step further by being more aggressive, which is shown by its lowercasing of character beyond the ASCII accepted characters. It even ignores the case of the given string, making it preferable to handle caseless string matching.

Syntax:

String_var.casefold()
Return Value:

The lower case version of the subject string.

The following example converts the string Captain AMERICA into lower case and results in output as captain america

			

# Python example of the casefold() method, 
txt ='Captain AMERICA'
x = txt.casefold()<br />
 
print(x)  
 
				

Output:

captain america

The casefold() method works more aggressively, converting even Unicode characters to lower case. In the following example, the casaefold() method converts the german letter ß to a smaller version ss while the lower() method does not convert it.

			

varstr = bbßBB'
print('Converted using the casefold() method:', varstr.casefold())
print('Converted using the lower() method:', varstr.lower())

				

Output:

Converted using the casefold() method: bbssbb Converted using the lower() method: bbßbb