casefold()
methodTo create caseless string matching, the Python String casefold()
technique is utilized.
The casefold()
method removes any case distinctions present in the given string. It’s used for caseless matching, which means it ignores cases when comparing.
The
casefold()
method is similar to thelower()
method, but more aggressive.
string.casefold()
The method casefold()
takes no parameters.
The casefold()
method returns the case folded string.
The following code shows how to use the casefold()
method in Python:
Convert string to lowercase using the casefold()
method.
string = "PYTHON IS A PROGRAMMING LANGUAGE"# print lowercase stringprint("Lowercase string:", string.casefold())
Compare strings using the casefold()
method.
str1 = "Fluß"str2 = "Fluss"# ß is equivalent to ssif str1.casefold() == str2.casefold():print('The strings are equal.')else:print('The strings are not equal.')
The German lowercase letter
ß
is equivalent toss
.
In the above code, the if
statement is passed the casefold()
method, which compares the strings. Under the hood, the casefold()
method automatically converts ß
to ss
, since it is equivalent.