casefold() string in Python

PythonServer Side ProgrammingProgramming

This function is helpful in converting the letters of a word into lowercase. When applied to two strings it can match their values irrespective of the type up of the case of the letters.

Applying casefold()

The below example we apply the casefold() function to a string and the result comes out in all lower case letters.

Example

string = "BestTutorials"
# print lowercase string
print(" lowercase string: ", string.casefold())

Output

Running the above code gives us the following result −

Lowercase String: besttutorials

Comparing using casefold()

We can compare two strings which have same letters but in different cases after applying the casefold() function. The result of the comparison gives a match of equality to the two words.

Example

string1 = "Hello Tutorials"
string2 = "hello tutorials"
string3 = string1.casefold()
if string2==string3:
   print("String2 and String3 are equal")
elif string1 != string3:
   print("Strings are not equal")

Output

Running the above code gives us the following result −

String2 and String3 are equal
raja
Published on 07-Aug-2019 08:10:47
Advertisements