Table of Contents Hide
Python String casefold() method is used to implement caseless string matching. Case folding is similar to lowercasing but more aggressive because the
casefold() function is more aggressive as it converts all string characters to lowercase. It is intended to remove all case distinctions in a string.
The syntax of
casefold() method is:
casefold() function does not take any parameters.
casefold() function returns a copy of the case folded string, i.e., the string is converted to lowercase. It doesn’t modify the original string.
Difference between casefold and lower in Python
lower() method converts all the uppercase characters in a string to lowercase characters, while the
casefold() method converts all the string characters into lowercase. In General, the
casefold() method removes all case distinctions present in a string.
For example, the German lowercase letter ‘ß‘ is equivalent to “ss“. Since ‘ß‘ is already lowercase, the
lower() method would do nothing to ‘ß‘; however,
casefold() still converts it to “ss“.
Example 1: Convert string to lowercase using casefold() method
text = "PYTHON CASEFOLD EXAMPLE" # Prints the lowercase string print ("Lowercase string is:", text.casefold())
Lowercase string is: python casefold example
Example 2: Compare strings using casefold() method
str1 = "Pythonß" str2 = "Pythonss" # ß in german is equivalent to ss if str1.casefold() == str2.casefold(): print('The given strings are equal.') else: print('The given strings are not equal.')
The given strings are equal.