Latest Code Tutorials

Python String casefold() Method: Complete Guide

Python string casefold() method is very similar to the lower() method. The casefold() method is used to implement caseless string matching.

Python String casefold()

Python String casefold() is a built-in function used to implement caseless string matching. The casefold() method 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 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 are both equivalents if we compare it using casefold.

Python String lowercase using casefold()

To convert a string to lowercase in Python, use the casefold() function. 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.

To check if a string is a palindrome in Python, use the casefold() and reversed() method.

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

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

It is another form of normalizing the text that may initially appear 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.

That’s it for this tutorial.

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.