How do I lowercase a string in Python?

How do I lowercase a string in Python?

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

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

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

>>> Kilometers.casefold()
kilometers

Heres why:

>>> Maße.casefold()
masse
>>> Maße.lower()
maße
>>> MASSE == Maße
False
>>> MASSE.lower() == Maße.lower()
False
>>> MASSE.casefold() == Maße.casefold()
True

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

Unicode Python 3

Python 3 handles plain string literals as unicode:

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

Python 2, plain string literals are bytes

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

And lower doesnt map any changes that bytes would be aware of, so we get the same string.

>>> string = Километр
>>> string
xd0x9axd0xb8xd0xbbxd0xbexd0xbcxd0xb5xd1x82xd1x80
>>> string.lower()
xd0x9axd0xb8xd0xbbxd0xbexd0xbcxd0xb5xd1x82xd1x80
>>> 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 string literal, which disambiguates with a u prefix (and note the u prefix also works in Python 3):

>>> 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
uu041au0438u043bu043eu043cu0435u0442u0440
>>> unicode_literal.lower()
uu043au0438u043bu043eu043cu0435u0442u0440

Now if we only have it in the form of a str, we need to convert it to unicode. Pythons 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
True

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
xd0x9axd0xb8xd0xbbxd0xbexd0xbcxd0xb5xd1x82xd1x80
>>> string.decode(utf-8)
uu041au0438u043bu043eu043cu0435u0442u0440
>>> string.decode(utf-8).lower()
uu043au0438u043bu043eu043cu0435u0442u0440
>>> string.decode(utf-8).lower().encode(utf-8)
xd0xbaxd0xb8xd0xbbxd0xbexd0xbcxd0xb5xd1x82xd1x80
>>> 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.

How do I lowercase a string in Python?

With Python 2, this doesnt 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()
километр

Leave a Reply

Your email address will not be published. Required fields are marked *