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.
# initialising the string string = "TUTORIALSPOINT" # printing the casefold() version of the string print(string.casefold())
If run the above program, you will get the following result.
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.
# initialising the string string = "ßtutorialspoint" second_string = "sstutorialspoint" # comparing two strings print(string == second_string)
As we expected the result of the above program is False.
Now, compare the same using the str.casefold() method.
# initialising the string string = "ßtutorialspoint" second_string = "sstutorialspoint" # comparing two strings print(string.casefold() == second_string)
If you run the above code, you will get the following results.
If you have any doubts regarding the tutorial, mention them in the comment section