Python: .lower() vs .casefold()

Essentially, both of these Python functions perform the same action: convert text to lowercase.

If there’s no difference, why… are there two of them?

The difference in how these two functions work is in the handling of certain special characters (examples are in the manual and online). Okay, so they handle characters that we won’t encounter in most practical situations differently. So when should we use one function or the other?

.lower() – We use it to display text to the user.
So, if we want to display lowercase text for the user to read, .lower() is our choice.

.casefold() – We use it for comparison.
If, for example, we need to search for a case-insensitive substring in a string, we reset the case using .casefold().

 

From my TG

0 0 votes
Article Rating
Subscribe
Notify of
0 Комментарий
Newest
Oldest Most Voted
Inline Feedbacks
View all comments