Remember
casefold() applies aggressive Unicode normalization of case for caseless matching. It can change characters that lower() leaves different.
casefold() applies aggressive Unicode normalization of case for caseless matching. It can change characters that lower() leaves different.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'Straße'
result = text.casefold()
print(result)Top-level scope
'Straße'Object 2 · str'strasse'casefold() applies aggressive Unicode normalization of case for caseless matching. It can change characters that lower() leaves different.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = 'Straße'
result = text.casefold()
print(result)
|
v
DISPLAYED RESULT
strasse
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (str)
object 1: 'Straße'
object 2: 'strasse'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'Straße'
result = text.casefold()
print(result)Top-level scope
'Straße'Object 2 · str'strasse'The example has finished. Compare the final output with the values and references above.
strasse
What do you expect this program to print?
text = 'Straße'
result = text.casefold()
print(result)casefold() MethodThe casefold() method in Python is used to convert a string into a case-insensitive comparable form. It is more aggressive than lower() and is recommended for comparisons that ignore case, especially in international text.
string.casefold()
casefold() does not take any parameters.str1 = "Straße"
str2 = "strasse"
print(str1.casefold() == str2.casefold())
True
Why? Because "ß" is casefolded to "ss" – something lower() doesn’t do.
text = "HELLO World"
print(text.casefold())
hello world
word = "Python"
user_input = "pYThOn"
if word.casefold() == user_input.casefold():
print("Match found!")
Match found!
| Feature | lower() |
casefold() |
|---|---|---|
| Converts to lowercase | Yes | Yes |
| Suitable for international comparison | No | Yes |
| More aggressive Unicode conversion | No | Yes |
casefold() on non-string data.str(your_variable).casefold()Use casefold() when asked to perform case-insensitive string comparison in a robust and Unicode-safe way.
casefold() is used for case-insensitive string comparison.lower().Write a function that checks if two user-entered strings are equal, ignoring case and accents.
def is_equal_case_insensitive(str1, str2):
return str1.casefold() == str2.casefold()
# Try it!
print(is_equal_case_insensitive("Straße", "STRASSE"))
True
Connect what you noticed in the example with the idea below.
casefold() applies aggressive Unicode normalization of case for caseless matching. It can change characters that lower() leaves different.
casefold() is always identical to lower().
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.