String casefold() in Python

In this article, we will be learning String casefold() in Python. In simple terms, the casefold() method is similar to the lower() method in Python but stronger.

Introduction –  The casefold() method does not take any arguments and case folds each character of a string. However, in general, it can be said as it converts a string into the lowercase but is more powerful and stricter while converting. The converted string, if compared, will give more case matches results than normal converted string from the lower() method.

Implementation of String casefold() in Python

For example, in german language, ß(alt key + 225), is translated to “ss”. The lower() method cannot convert as it ignores case conversion but while using casefold() it converts into “ss”.

Syntax:- string.casefold()

Returns:- It does not return anything.

  1. Converting a normal string to lowercase using casefold()
    str = " Hello WORLD"
    str = str.casefold()
    str

    As a result, we get the following output:-

    ' hello world'

    You can see here as it works similarly like the lower() method.

  2. Difference between casefold() and lower()
    str = "STrAße"
    str1 = str.casefold()
    str2 = str.lower()
    print(str1,str2)

    As a result, we get the following output:-

    strasse straße

    As you can see the difference, casefold() strictly converts “ß” to “ss” along with other characters whereas the lower() method only converts capital letters of the string to lowercase.

I hope you like the article, feel free to comment down your queries.

Leave a Reply