What is the casefold() function in Python?
The Python casefold() method is a built-in function that converts a string into lowercase. It is one of the many built-in Python functions readily available in the Python library. This function is known for its caseless matching ability. That is, it can ignore cases when comparing, and it removes all the case distinctions that are present in the string.
Syntax
The syntax for the
casefold() function is shown below:
string.casefold()
Parameters and return value
The casefold() method returns a case folded string without any parameters.
casefold() vs lower()
The casefold() function is similar to the lowercase() function in Python. So why bother using the casefold() function?
- It converts all characters in a string to lowercase
- It ignores cases when comparing
- It is suited for caseless matching
- It is best for unicode characters that
lower()does not change
Code
The sample code shown below converts a string in capital case to lowercase:
# Converts string to lowercasesample_string = "HELLO WORLD"print(sample_string.casefold())
Another example shown below compares two strings and checks if they are equal:
first_text = "This is a sample text"second_text = "This Is A Sample Text"if first_text.casefold() == second_text.casefold():print("The strings are equal")else:print("The strings are not equal")