Python String casefold() Method

Python String casefold() method is used to convert all characters of the string into lowercase letters.

This method is similar to the lower() method, but the casefold() performs more aggressive, culture-independent case normalization, especially for characters in various languages. 

If your project requires localization, we recommend using the casefold() method.

Syntax

string.casefold()

Parameters

This method doesn’t take any parameter.

Return value

It returns a string converted in the lower case.

Visual RepresentationVisual Representation of Python String casefold() MethodExample 1: How to Use String casefold() Method

string = "APPDIVIDEND"
print("Uppercase string:", string)

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

Output

Uppercase string: APPDIVIDEND
Lowercase String: appdividend

Example 2: Using non-ASCII characters

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")
else:
  print("Both the strings are not same")

Output

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

Both the strings are the same.

In the second example, we can see that ß(lowercase) in German letter and ss in English are equivalents if we compare them using casefold.

Symbols and Letters are not affected by the casefold() method. If the string is lowercase, it will return the original string.

Example 3: Difference between casefold() and lower()Visual Representation of Difference between casefold() and lower() method

firststring = "fuß"

print("Using casefold method:", firststring.casefold())
print("Using lower method:", firststring.lower())

Output

Using casefold method: fuss
Using lower method: fuß

Leave a Comment

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