casefold(): Convert string to lower case chars

my_str="Welcome to Plus2net Python"
print(my_str.casefold())
Output is here
welcome to plus2net python
We can user lower() method also to change all chars to lower case but casefold() is more powerful. This can be used for string case insensitive comparison when other than plain English text are used.

casefold() and lower() difference

my_str="aBcßeF"
print(my_str.casefold())
print(my_str.lower())
Output is here
abcssef
abcßef

Use Case: Case-Insensitive String Comparison

casefold() is ideal when comparing strings in a case-insensitive manner, especially for non-English text:

str1 = "Straße"
str2 = "STRASSE"
print(str1.casefold() == str2.casefold())  # Output: True

Difference Between lower() and casefold()

casefold() is more powerful than lower() as it can handle more complex characters:

text = "aBcß"
print(text.lower())  # Output: abcß
print(text.casefold())  # Output: abcss

Use Case: Normalizing Input for Case-Insensitive Search

Use casefold() to normalize input for a search function, allowing for accurate case-insensitive searches:

search_term = "PYTHON"
content = "Learning python programming."
if search_term.casefold() in content.casefold():
    print("Search term found!")# Output: 'Search term found!'

Working with Non-English Characters

casefold() handles non-ASCII characters effectively:

text = "ßß"
print(text.casefold())  # Output: 'ss'

Case-Insensitive String Comparison Across Languages

word1 = "Τεχνολογία"
word2 = "τεχνολογία"
print(word1.casefold() == word2.casefold())  # Output: True


All String methods


Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer