How do I convert a string to lowercase in Python?

Python, String · Jun 12, 2021


Python's standard method for converting a string to lowercase is str.lower() and is compatible with both Python 2 and Python 3. While this is the standard way for most cases, there are certain cases where this method might not be the most appropriate, especially if you are working with Unicode strings.

'Hello'.lower()               # 'hello'
'Straße'.lower()              # 'straße'
'Straße'.upper().lower()      # 'strasse'
# Example of incorrect result when used for unicode case-insensitive matching
'Straße'.upper().lower() == 'Straße'.lower() # False ('strasse' != 'straße')


Python 3 introduced str.casefold(), which is very similar to str.lower(), but more aggressive as it is intended to remove all case distinctions in Unicode strings. It implements the casefolding algorithm as described in section 3.13 of the Unicode Standard.

'Hello'.casefold()            # 'hello'
'Straße'.casefold()           # 'strasse'
'Straße'.upper().casefold()   # 'strasse'
# Returns the correct result when used for unicode case-insensitive matching
'Straße'.upper().casefold() == 'Straße'.casefold() # True

More like this

  • Hex to RGB

    Converts a hexadecimal color code to a tuple of integers corresponding to its RGB components.

    Python, String · Sep 15, 2020

  • RGB to hex

    Converts the values of RGB components to a hexadecimal color code.

    Python, String · Nov 2, 2020

  • How do I trim whitespace from a string in Python?

    Oftentimes you might need to trim whitespace from a string in Python. Learn of three different way to do this in this short guide.

    Python, String · Dec 13, 2021