How do I lowercase a string in Python?


Question

Is there a way to convert a string from uppercase, or even part uppercase to lowercase?

For example, "Kilometers" → "kilometers".

1
1854
5/31/2019 4:06:08 PM

Accepted Answer

Use .lower() - For example:

s = "Kilometer"
print(s.lower())

The official 2.x documentation is here: str.lower()
The official 3.x documentation is here: str.lower()

2848
2/13/2019 4:23:25 PM

With Python 2, this doesn't work for non-English words in UTF-8. In this case decode('utf-8') can help:

>>> s='Километр'
>>> print s.lower()
Километр
>>> print s.decode('utf-8').lower()
километр

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon