How to convert string to lowercase in Python

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

E.g. Kilometers --> kilometers.



answered 7 years ago Petar Ivanov #1

s = "Kilometer"

The official documentation is str.lower().

answered 7 years ago user735977 #2

You can do what Peter said, or if you want the user to input something you could do this:

raw_input('Type Something').lower()

It will then automatically convert the thing they typed into lowercase.

Note: raw_input was renamed to input in Python 3.x and above.

answered 4 years ago ergo #3

Also, you can overwrite some variables:

s = input('UPPER CASE')
lower = s.lower()

If you use like this:

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

It will work just when called.

answered 3 years ago Vladimir Gorovoy #4

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()

answered 3 years ago Aaron Hall #5

How to convert string to lowercase in Python?

Is there any way to convert an entire user inputted string from uppercase, or even part uppercase to lowercase?

E.g. Kilometers --> kilometers

The canonical Pythonic way of doing this is

>>> 'Kilometers'.lower()

However, if the purpose is to do case insensitive matching, you should use case-folding:

>>> "MASSE" == "Maße"
>>> "MASSE".lower() == "Maße".lower()
>>> "MASSE".casefold() == "Maße".casefold()

This is a str method in Python 3, but in Python 2, you'll want to look at the PyICU or py2casefold - several answers address this here.

Unicode Python 3

Python 3 handles unicode as regular strings:

>>> string = 'Километр'
>>> string
>>> string.lower()

Unicode Python 2

But Python 2 does not, the below, pasted into a shell, encodes the literal as a string of bytes, using utf-8.

And lower doesn't map any changes that native Unicode objects would be aware of, so we get the same string.

>>> string = 'Километр'
>>> string
>>> string.lower()
>>> print string.lower()

In scripts, Python will object to non-ascii (as of Python 2.5, and warning in Python 2.4) bytes being in a string with no encoding given, since the intended coding would be ambiguous. For more on that, see the Unicode how-to in the docs and PEP 263

Use Unicode literals, not str literals

So we need a unicode string to handle this conversion, accomplished easily with a unicode literal:

>>> unicode_literal = u'Километр'
>>> print unicode_literal.lower()

Note that the bytes are completely different from the str bytes - the escape character is '\u' followed by the 2-byte width, or 16 bit representation of these unicode letters:

>>> unicode_literal
>>> unicode_literal.lower()

Now if we only have it in the form of a str, we need to convert it to unicode. Python's Unicode type is a universal encoding format that has many advantages relative to most other encodings. We can either use the unicode constructor or str.decode method with the codec to convert the str to unicode:

>>> unicode_from_string = unicode(string, 'utf-8') # "encoding" unicode from string
>>> print unicode_from_string.lower()
>>> string_to_unicode = string.decode('utf-8') 
>>> print string_to_unicode.lower()
>>> unicode_from_string == string_to_unicode == unicode_literal

Both methods convert to the unicode type - and same as the unicode_literal.

Best Practice, use Unicode

It is recommended that you always work with text in Unicode.

Software should only work with Unicode strings internally, converting to a particular encoding on output.

Can encode back when necessary

However, to get the lowercase back in type str, encode the python string to utf-8 again:

>>> print string
>>> string
>>> string.decode('utf-8')
>>> string.decode('utf-8').lower()
>>> string.decode('utf-8').lower().encode('utf-8')
>>> print string.decode('utf-8').lower().encode('utf-8')

So in Python 2, Unicode can encode into Python strings, and Python strings can decode into the Unicode type.

answered 1 year ago Supercolbat #6

If the whole text is uppercase like "KILOMETER", and you only want the first character to be lowercased, then do

text = "KILOMETER"
result = text[:1] + text[1:].lower() 

But to lowercase the whole string, do

text = "KILOMETER"
text = text.lower()

answered 3 months ago anand tripathi #7

Some basic python string methods are explained below ##:


The string lower() method converts all uppercase characters in a string into lowercase characters and returns it.


string = "HeLLO PyTHON"
new_string = string.lower()

print string
print new_string


hello python


In Python, the capitalize() method converts the first character of a string to capital (uppercase) letter.


string = "hello python"
new_string = string.capitalize()

print string
print new_string


hello python
Hello python


The string swapcase() method converts all uppercase characters to lowercase and all lowercase characters to uppercase characters of the given string and returns it.

string = "HEllO PythOn"
new_string = string.swapcase()

print string
print new_string


HEllO PythOn
heLLo pYTHoN

answered 4 weeks ago Shreyansh Kumar #8

It will convert the string to lower case

string = "XYz"
converted = string.lower();
print("The converted lower case is:",converted)

answered 6 days ago josiah #9

If you want to use map to perform this operation on multiple strings in a list you will need to use str.lower and not merely lower:

words = ['CATS', 'KITTENS', 'Pirate Cats', 'fluffy felines']
list(map(str.lower, words))


['cats', 'kittens', 'pirate cats', 'fluffy felines']

comments powered by Disqus