casefold() string in Python Program


In this tutorial, we are going to discuss the string method str.casefold(). It doesn't take any arguments. The return value of the method is a string that is suitable for the caseless comparisons.

What are caseless comparisons? For example, the german lower case letter ß is equivalent to ss. The str.casefold() method returns the ß as ss. It converts all the letters to lower case.

Example

 Live Demo

# initialising the string
string = "TUTORIALSPOINT"
# printing the casefold() version of the string
print(string.casefold())

Output

If run the above program, you will get the following result.

tutorialspoint

Let's see the example where caseless comparison works. If you directly compare the string ßtutorialspoint with sstutorialspoint, we will get False as output. Let's see the code.

Example

 Live Demo

# initialising the string
string = "ßtutorialspoint"
second_string = "sstutorialspoint"
# comparing two strings
print(string == second_string)

Output

As we expected the result of the above program is False.

False

Now, compare the same using the str.casefold() method.

Example

 Live Demo

# initialising the string
string = "ßtutorialspoint"
second_string = "sstutorialspoint"
# comparing two strings
print(string.casefold() == second_string)

Output

If you run the above code, you will get the following results.

True

Conclusion

If you have any doubts regarding the tutorial, mention them in the comment section

Updated on: 02-Jan-2020

60 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements