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.

casefold() Syntax

The syntax of casefold() method is:

string.casefold()

casefold() Parameter

The casefold() function does not take any parameters.

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

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

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

Get notified of the best deals on our WordPress themes.

You May Also Like
List of Python free ebooks

List of Free Python Books

Table of Contents Hide Python SuccinctlyPython Data Science HandbookThink Python 2nd EditionAutomate the Boring Stuff with PythonMaking Games with Python & PygameData Structures and Algorithms in Python Google Python Style GuideA…
View Post