Python String casefold()

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.

Syntax

The syntax of casefold() method is:

string.casefold()

Parameter

The casefold() function does not take any parameters.

Return Value

The 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

The 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())

Output

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.')

Output

The given strings are equal.
Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like