In this tutorial, we’ll learn what Python string casefold() method is and how to properly use it with the help of an easy Python code examples.
Introduction: Python String casefold() Method
This method is used to return a new string having all the characters converted to lowercase format.
Syntax of casefold() Method
string.casefold()
- This method does not take any arguments.
- Python string casefold() method returns a lowercased new string.
Example 1: casefold() applied on a Python String
statement='ThIs COlor IS BluE' print(statement.casefold())
Output
this color is blue
As you can see here, all the characters of a string are converted to lowercase.
Example 2: casefold() is a Powerful Method
statement='THIS COLOR Is BluEß' print(statement.casefold()) print(statement.lower())
Output
this color is bluess // using casefold() method this color is blueß // using lower() method
Python string casefold() method is more powerful than lower() method. We have used a German letter ß in our string which is already lowercased but Python string casefold() method will convert it to its equivalent character ss as seen in the above code output. Using lower() method on it won’t have any effect on that German character as its already lowercased.
Example 3: casefold() Method Return Value
originalValue='I AM A PAKISTANI' returnedString=originalValue.casefold() print(returnedString) print(originalValue)
Output
i am a pakistani // new string I AM A PAKISTANI // original string
Python string casefold() method does not modify the existing string. It returns a new string having all the characters in lowercase format.
Conclusion
As a conclusion of this tutorial, now you have a detailed and in-depth practical knowledge of how to easily use Python string casefold method. I’ll be looking forward to have your amazing feedback. Thank you for reading this post.