Latest Code Tutorials

Python String casefold() Method Example


Python string casefold() method is very much similar to the lower() method. Sometimes it is also called the aggressive version of the lower() method. This string method is used to implement caseless string matching. It removes all the case distinctions of the string. It means that it ignores cases while comparing the string.

Python String casefold()

Python casefold() string method is used to implement caseless string matching. It is similar to the lower() string method, but the case removes all the case distinctions present in a string. i.e., ignore cases when comparing.

Python casefold() method removes all case distinctions present in the string.

It is used for caseless matching, i.e., ignores cases when comparing.




Python casefold() doesn’t take any parameter.

Return value

Python casefold() method returns a string that is converted in the lower case.

Example programs on the method casefold()

Write a program to show the mechanism of casefold().


print("Uppercase string:", string)
print("Lowercase String: ", string.casefold())

See the output.

➜  pyt python3
Uppercase string: HELLO FROM DAVID LETTERMAN! :)
Lowercase String:  hello from david letterman! :)
➜  pyt

Example 2: Write two strings and compare them using casefold().


firststring = "light the bulß"
secondstring = "light the bulss"
print("First string:", firststring)
print("Second string:", secondstring)
print("After comparing it using casefold:")
if(firststring.casefold() == secondstring.casefold()):
    print("Both the strings are same")
    print("Both the strings are not same")

See the output.

➜  pyt python3
First string: light the bulß
Second string: light the bulss
After comparing it using casefold:
Both the strings are same
➜  pyt

Both the strings are the same

In the second example, we can see that ß in german and ss in English both are equivalent if we compare it using casefold.

#Python String lowercase using casefold()

See the following code.


string = "ROKU IS AWESOME"

# print lowercase string
print("Lowercase string:", string.casefold())

See the output.

➜  pyt python3
Lowercase string: roku is awesome
➜  pyt

#Check if a string is palindrome in Python

See the following program.


str = 'abcdcba'

# make it suitable for caseless comparison
str = str.casefold()

# reverse the string
rev_str = reversed(str)

# check if the string is equal to its reverse
if list(str) == list(rev_str):
    print(" not palindrome")

See the output.

➜  pyt python3
➜  pyt

#Python: lower() vs. casefold()

Casefolding is the more aggressive version of the lower() that is set up to make many of the more unique Unicode characters more comparable.

It is an another form of normalizing the text that may initially appear to be very different, but it takes the characters of many different languages into account.

If you are working strictly in the English language, lower() and casefold() should be yielding precisely the same results.

However, if you are trying to normalize the text from other languages that use more than our simple 26-letter alphabet (using only ASCII), I would use the casefold() to compare your strings, as it will yield more consistent results.

Recommended Posts

Python String expandtabs()

Python String endswith()

Python dict() method

Python hex() function

Python String center()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.