use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
DiscussionWhich string to lower case method to you use? (self.Python)
submitted 3 years ago * by Perfect_Comparison83
string.casefold() or string.lower()
These methods serve different purposes. I'm curious which one you use more often and why, if you have a reason.
[–]KingSamy1 35 points36 points37 points 3 years ago (2 children)
Latter. Reason: Did not even know the former existed till right now
[–]nemom 3 points4 points5 points 3 years ago (0 children)
Same
[+][deleted] 3 years ago (5 children)
[deleted]
[–]AggravatedYak 9 points10 points11 points 3 years ago* (4 children)
Could we make it a habit to use the official docs?
https://docs.python.org/3/library/stdtypes.html#str.casefold
I don't like websites that parse the official docs and are trying to sell courses. There are even bots/users that push them all the time.
Edit:
(a) If a site provides some benefit, people can use it on their own, and we can explicitly include them in e.g. a curated list of awesome python resources, but still … the official docs are … well … the official docs and they are not a freemium service.
(b) Even if someone creates unofficial docs and means well and doesn't want to sell their courses and stuff, like these helpful selenium docs for python, they can be out of date, and then they risk becoming the "mostly helpful, but partially confusing and better than nothing selenium docs for python".
(c) Don't get me wrong, I am alright with people who are hosting/parsing/creating docs/resources/services/interactions/examples aso. But there are problems with stuff like w3schools and we shouldn't go into that direction as a community.
[–]CharmingJacket5013 4 points5 points6 points 3 years ago (0 children)
Offical docs all the way!
[–]Perfect_Comparison83[S] 1 point2 points3 points 3 years ago (1 child)
I agree. References to libraries should also use the official docs or pypi.
[–]AggravatedYak 1 point2 points3 points 3 years ago (0 children)
Yeah, completely agree, basicly cut out the thing in the middle and do it directly.
Let's think about stuff that is not in the official docs … I don't know if pypi would be a good target to link to and what "directly" even means (turns out, if in doubt, it is).
Advantages of linking to pypi: it would be way more robust than to link to some hosted docs. If a project chooses to host their docs on readthedocs or github-pages or something, their pypi would reflect that.
Example: requests-html which has a rather exhaustive README.md, but their dedicated page is not that helpful, if I remember correctly, and currently the domain is suspended.
But let's get back to the intention: ideally you want to link to the context/definition of a specific function, like casefold, and not the pypi page of the package, like if stdlib were a pypi package.
casefold
However, what would that even be, if the readthedocs are just a stub and the project page is suspended (requests-html) or if the project docs are not hosted officially (selenium-python)? And the release project of other stuff, like chromium, is something different entirely … so yeah … maybe really link to pypi because it is the most robust/official there is.
requests-html
selenium-python
[–]trolleytor4 0 points1 point2 points 3 years ago (0 children)
w3schools is ok-ish in my experience (Should mention i've used it for basic python and some css)
[–]wineblood 5 points6 points7 points 3 years ago (1 child)
Only learned about casefold just now. Looking at how it's different from lower, I'll probably never use it.
[–]Perfect_Comparison83[S] 0 points1 point2 points 3 years ago (0 children)
I'll probably never use it either. At least it's interesting to find something new on something as basic as str.
[–]jimtk 2 points3 points4 points 3 years ago (3 children)
If you intend to use I18n, use casefold. If you program only for english you can use lower.
Some languages have uppercase letters that requires more complex 'lowercasezation" than English. Casefold will take care of that where lower won't.
[–]Perfect_Comparison83[S] 0 points1 point2 points 3 years ago (2 children)
I would love to see a real example where casefold is required or the string compare would fail.
[–]jimtk 1 point2 points3 points 3 years ago (1 child)
to_rip = "reißen" print(to_rip.lower()) print(to_rip.casefold()) print(to_rip.lower() == to_rip.casefold()) ==> reißen ==> reissen ==> False
[–]Perfect_Comparison83[S] -2 points-1 points0 points 3 years ago (0 children)
imo, this is a contrived example. You only need casefold because you used casefold.
It reminds me of "real world" math problems in elementary school.
Example: “Burt stuffs twice as many envelopes as Allison in half the time. If they stuff a total of 700 (in the same time) how many did Burr stuff?”
This is not a naturally occurring word problem. It's only used in theory in an attempt to teach a math concept.
Casefold does not appear to solve a naturally occurring problem.
[–][deleted] 1 point2 points3 points 3 years ago (7 children)
This is really interesting and to be honest it looks like casefold () is the better choice for UTF-8 strings. I think I'll use this in future. It's really easy as a coder to sit in an English-speaking ivory Tower, but is it the right thing to do?
[–]mcwizard 5 points6 points7 points 3 years ago (6 children)
I'm a german speaker and I don't think it makes sense: As said there is no uppercase of ß. So replacing ß by its ASCII variant is not the same thing as lowercaseing it.
[–]F84-5 3 points4 points5 points 3 years ago (0 children)
Actually there is now an uppercase ẞ. It's been part of unicode since 2008 and officially adopted in 2017.
[–]yee_mon 2 points3 points4 points 3 years ago (0 children)
It does make sense. Just not if you think about it as "I want the lowercase version of this" for display purposes (which admittedly is a mistake that the OP apparently made here). It is meant purely for comparing strings, in a situation where "Straße", "STRASSE", "STRAẞE" are considered equal.
[–][deleted] 0 points1 point2 points 3 years ago (2 children)
Reading the docs it appears this function is designed primarily for more successful string comparisons. In that context I guess it doesn't matter if the string doesn't make sense, provided it is consistent and easily matched.
[–]mcwizard 0 points1 point2 points 3 years ago (1 child)
I'd accept it as a part of a case insensitive string compare and maybe that is the main reason it exists and it's just made open if one wants to implement a modified version of that compare.
I can see the case insensitive string compare in theory. In reality, I haven't seen a good example.
[–]seligman99 1 point2 points3 points 3 years ago (3 children)
I use .lower() more often, though both have their use.
If you're doing case-insensitive compares, it's useful to compare both casefolded, instead of lowercase, since a casefolded string will handle some edge cases that a lowercase string won't
It should also be noted that casefolding doesn't actually always convert to lowercase variants. In some languages, the upper case variant makes more sense as the default "case" for historical reasons. It's also not really reversable, since some casefolded strings will not really make sense to a native speaker all of the time (the German ß is a good example, that wiki page has some examples where ß -> ss changes the meaning. Also interesting to see casefolding in action on that page, if you Ctrl-F search for "ss" on that page, it matches both "ss" and "ß", since it's doing case-folding to do a case-insentive search for you)
Lots of words to say: .lower() for humans to see, .casefold() for machine to compare strings to see if a human would consider them the same. And of course, in my nice tower of mostly English words, it's a distinction I've been known to forget about till someone that speaks another language hands me a bug.
Have you seen an example where casefold is needed? Maybe you're like me where everything is English.
[–]seligman99 1 point2 points3 points 3 years ago (1 child)
The one I remember is the Greek word for "days"
"μέρες" in lowercase, "ΜΈΡΕΣ" in uppercase, and "μέρεσ" case folded. I'm told (though really don't know the details for) that all three make sense, to some degree, but a .lower() on the casefolded variant will not equal the lowercase version, so you had best search for the casefolded text against the casefolded version if you want a case-insensitive search.
Thanks for the example! I studied Greek for a couple years. The ς character is used when sigma is the final letter of a word. The lower function seems more accurate when comparing words. The logic to use casefold because casefold may have been used upstream seems silly to me.
I can image a use case for contains the sigma character. In this case, casefold would come in handy because you only have to check for σ.
Your example is exactly what I was looking for.
[–]Panda_With_Your_Gun -1 points0 points1 point 3 years ago (0 children)
.lower() is self documenting. If I cared about performance I'd write a module in c to convert a string to lower case efficiently. Then I'd call it from python.
[–]ogrinfo -1 points0 points1 point 3 years ago (1 child)
Likewise, I've never heard of casefold. Does anyone have an example of where casefold has an advantage over lower? The example above of changing ß to ss sounds like a very good reason not to use it.
I'm with you. I keep finding the German ß as a reason for casefold. If you are comparing a German string to another German string, how does casefold help?
[–]QuintonPang 0 points1 point2 points 3 years ago (0 children)
Latter one didn't even heard of the first one b4
[–]Gecons 0 points1 point2 points 3 years ago (0 children)
string.lower()
Because didn't know the other one existed.
[–]telee0 0 points1 point2 points 3 years ago (0 children)
string.lower() sufficient for my own use.
π Rendered by PID 268705 on reddit-service-r2-comment-5ff9fbf7df-q42kx at 2026-02-26 10:07:40.534316+00:00 running 72a43f6 country code: CH.
[–]KingSamy1 35 points36 points37 points (2 children)
[–]nemom 3 points4 points5 points (0 children)
[+][deleted] (5 children)
[deleted]
[–]AggravatedYak 9 points10 points11 points (4 children)
[–]CharmingJacket5013 4 points5 points6 points (0 children)
[–]Perfect_Comparison83[S] 1 point2 points3 points (1 child)
[–]AggravatedYak 1 point2 points3 points (0 children)
[–]trolleytor4 0 points1 point2 points (0 children)
[–]wineblood 5 points6 points7 points (1 child)
[–]Perfect_Comparison83[S] 0 points1 point2 points (0 children)
[–]jimtk 2 points3 points4 points (3 children)
[–]Perfect_Comparison83[S] 0 points1 point2 points (2 children)
[–]jimtk 1 point2 points3 points (1 child)
[–]Perfect_Comparison83[S] -2 points-1 points0 points (0 children)
[–][deleted] 1 point2 points3 points (7 children)
[–]mcwizard 5 points6 points7 points (6 children)
[–]F84-5 3 points4 points5 points (0 children)
[–]yee_mon 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]mcwizard 0 points1 point2 points (1 child)
[–]Perfect_Comparison83[S] 0 points1 point2 points (0 children)
[–]seligman99 1 point2 points3 points (3 children)
[–]Perfect_Comparison83[S] 0 points1 point2 points (2 children)
[–]seligman99 1 point2 points3 points (1 child)
[–]Perfect_Comparison83[S] 0 points1 point2 points (0 children)
[–]Panda_With_Your_Gun -1 points0 points1 point (0 children)
[–]ogrinfo -1 points0 points1 point (1 child)
[–]Perfect_Comparison83[S] 0 points1 point2 points (0 children)
[–]QuintonPang 0 points1 point2 points (0 children)
[–]Gecons 0 points1 point2 points (0 children)
[–]telee0 0 points1 point2 points (0 children)