str.lower()
Returns a copy of a string with every capital letter turned into a small (lower-case) letter. Useful for comparing text.
str.lower()lower() turns every capital letter into its small form: "Hello World" becomes "hello world". Digits, spaces and punctuation stay as they are.
Its main job is making comparisons fair. People type "Yes", "YES" or "yes"; after lower() they all look the same. It is also the usual clean-up for e-mail addresses and tags.
For text in any language there is a stronger version, str.casefold(). For example "Straße".lower() keeps the ß, so it does not equal "STRASSE".lower(), while casefold() makes the two match.
Return value
A new string in small letters. The original string does not change.
Examples
1Lower-case a sentence
text = "Hello World, I am LEARNING Python"
print(text.lower())hello world, i am learning python
2Accept any spelling of an answer
answer = input("Continue? ")
if answer.strip().lower() in ("y", "yes"):
print("Going on")
else:
print("Stopped")Continue? Going on
The prompt is printed, then the result; the typed YES is not part of the output.
3Normalise e-mail addresses
emails = ["[email protected]", "[email protected]", "[email protected]"]
unique = sorted(set(e.lower() for e in emails))
print(unique)['[email protected]', '[email protected]']
4Count words in any case
text = "Tea or tea? TEA!"
words = text.lower().replace("?", "").replace("!", "").split()
print(words.count("tea"))3
Common mistakes
Lower-casing only one side of a comparison
Both sides must be in the same case. If you compare with a text that contains capitals, it can never match.
city = "Paris"
print(city.lower() == "Paris")
print(city.lower() == "paris")False True
Questions people ask
lower() vs casefold(): which should I use?
lower() is fine for display and for most English text. For comparing text that may come from any language, str.casefold() handles more special letters.
How do I check if a string is all lower case?
Use text.islower().
Does lower() change numbers or symbols?
No. Only letters that have a small form change.