lowercase python string uppercase

How do I lowercase a string in Python?


Is there a way to convert a string to lowercase?

"Kilometers"  →  "kilometers"



    Use str.lower():



    • 3

      This only works well with ASCII characters, you may want to use str.maketrans and str.translate if you are not getting the expected string.

      Dec 29, 2020 at 7:38

    • 2

      Not only ASCII, it works for many diacritics, for example ÀÇÐÊĞİŃÓŒŘŠŤÚŻ but there is a problem for dotless i "ı".upper().lower() becomes i, while upper dotted is conserved thanks to a Combining dot above (0x307).

      – lolesque

      Mar 7 at 13:57



    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:

    >>> 'Kilometers'.casefold()

    Here’s why:

    >>> "Maße".casefold()
    >>> "Maße".lower()
    >>> "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 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 doesn’t map any changes that bytes 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 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
    >>> 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.


    • I have one note that doesn’t necessarily apply to the OP’s question, but which is important with portability (internationalization) when doing case insensitive matching. With case-insensitive matching, diacritics (accent marks) may become a concern. Example: >>> "raison d'être".casefold(); "raison d'être" Check out this answer about unidecode

      Jul 23, 2018 at 17:27


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


    • 11

      Perhaps we should be a bit more explicit by saying that the decode('utf-8') is not only unnecessary in Python 3, but causes an error. (ref). Example: $python3; >>>s='Километр'; >>>print (s.lower); #result: километр >>>s.decode('utf-8').lower(); #result: ...AttributeError: 'str' object has no attribute 'decode' We can see a second way to do this, referencing the excellent answer of @AaronHall. >>>s.casefold() #result: километр

      Jul 23, 2018 at 17:16