The Python casefold method generates a lowercase version of the string. It is quite similar to the lowercase method; however, it goes a step further by eliminating all case distinctions found within the string.
As an illustration, in the German language, the character 'β' corresponds to "ss". Given that it is already in its lowercase form, applying lowercase operations does not alter it and simply results in 'β', while the casefold method transforms it into "ss".
Python String casefold Method Syntax
It has the following syntax:
casefold()
Parameters
No parameter is required.
Return Type
It returns lowercase string.
Python Version
This function was introduced in Python 3.3 .
Different Examples for Python Sring casefold Function
In this section, we will explore several examples to illustrate how the string casefold function operates in Python.
Python String Casefold Method Example 1
To demonstrate the functionality of the string casefold method in Python, let's consider an example.
# Python casefold() function example
# Variable declaration
str = "LOGICPRACTICE"
# Calling function
str2 = str.casefold()
# Displaying result
print("Old value:", str)
print("New value:", str2)
Output:
Old value: LOGICPRACTICE
New value: logicpractice
Python String Casefold Method Example 2
The advantage of using casefold lies in its capability to transform text into lowercase in a strict manner. For instance, when we apply casefolding to the character 'β', it is transformed into "ss".
# Python casefold() function example
# Variable declaration
str = "LOGICPRACTICE - β"
# Calling function
str2 = str.casefold()
# Displaying result
print("Old value:", str)
print("New value:", str2)
Output:
Old value: LOGICPRACTICE - β
New value: logicpractice ? ss
Python String Casefold Method Example 3
Even if a string is formatted in camelCase, the entire string is still transformed into lowercase.
# Python casefold() function example
# Variable declaration
str = "logicpractice"
# Calling function
str2 = str.casefold()
# Displaying result
print("Old value:", str)
print("New value:", str2)
Output:
Old value: logicpractice
New value: logicpractice