How to Lowercase a String in Python?

Rate this post

To convert a string to a lowercase string in Python, use the string.lower() built-in string method. This returns a lowercase string version.

>>> 'FINXTER'.lower()
'finxter'

As you read over the explanations below, feel free to watch our video guide about this particular string method:

If you want to perform case insensitive matching in languages such as German or Russian with special symbols, you should use the string.casefold() method that returns the lowercase version of a string but is more aggressive by converting, for example, the 'ß' character to a lowercase 'ss' as well.

>>> 'Finxter'.casefold()
'finxter'

Here are some examples where the output of casefold() and lower() is actually different.

>>> 'Fluß'.casefold()
'fluss'
>>> 'Fluß'.lower()
'fluß'

str.lower() – Syntax and Explanation

str.lower()

Returns a lowercase string version.

Here are a few examples:

>>> 'FINXTER'.lower()
'finxter'
>>> 'Finxter'.lower()
'finxter'
>>> 'FinXter TEACHES Python'.lower()
'finxter teaches python'
>>> 'ۧp$'.lower()
'ۧp$'

You can find the full algorithm for lowercasing a string in the section 3.13 of the Unicode standard in this PDF document.

str.casefold() – Syntax and Explanation

str.casefold()

Return a lowercased, casefolded string similar to lower() but more aggressive because it removes all case distinctions. For example, the German lowercase letter 'ß' is already lowercase and 'ß'.lower() wouldn’t change it. But because 'ß' is equivalent to 'ss', str.casefold() converts 'ß' to 'ss'.

The casefolding idea and algorithm is described in detail here:

Case mapping is the process of transforming characters to a specific case, such as UPPER, lower, or Titlecase. For those scripts that have a case distinction, Unicode defines a default UPPER, lower, and Titlecase character mapping for each Unicode code point. Case mapping, at first, appears simple. However there are variations that need to be considered when treating the full range of Unicode in diverse languages.

Case folding is the process of making two texts which differ only in case identical for comparison purposes, that is, it is meant for the purpose of string matching. This is distinct from case mapping, which is primarily meant for display purposes. As with the default case mappings, Unicode defines default case fold mappings (“case folding”) for each Unicode code point. Unicode defines two forms of case folding, which we’ll examine below.

Unpythonic But Fun Way

It would probably be best if you stopped reading now.

Still here? Okay, here’s an alternative way that is NOT recommended. It’s overly complicated, unreadable, and error-prone. But it provides you a bit more fine-grained control if you need to convert only some uppercase characters to lowercase ones or otherwise customize the behavior of lowercasing a string.

import string

s = 'FINXTER'
lower = ''.join([string.ascii_lowercase[string.ascii_uppercase.index(c)] for c in s])

print(lower)
# finxter

This method uses list comprehension [ <expression> for c in s] to convert each character to its lowercase version. The expression achieves this in two steps:

  • string.ascii_uppercase.index(c) – determine the index of the character in the uppercase string using the index() method.
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  • string.ascii_lowercase[...] – determine the respective character in the lowercase string using simple string indexing.
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'

However, this method fails if you run it with a string that is not all uppercase as it won’t find the uppercase character in the first step.

Didn’t I tell you not to use this method? 😉

More String Methods

Python’s string class comes with a number of useful additional string methods. Here’s a short collection of all Python string methods—each link opens a short tutorial in a new tab.

MethodDescription
capitalize()Return a copy of the string with capitalized first character and lowercased remaining characters.
casefold()Return a lowercased, casefolded string similar to lowercase() but more aggressive.
center()Return a centered string of a certain length, padded with whitespace or custom characters.
count()Return the number of non-overlapping occurrences of a substring.
encode()Returns a byte object that is an encoded version of the string.
endswith()Returns whether the string ends with a given value or not (True or False).
expandtabs()Return a string with spaces instead of tab characters.
find()Returns the index of the first occurrence of the specified substring.
format()Formats the string according to the Format Description Language.
format_map()Formats the string according to the Format Description Language, passing a mapping object.
index()Returns the index of the first occurrence of the specified substring, like find() but it raises a ValueError if the substring is not found.
isalnum()Checks whether all characters are alphabetic or numeric (True or False).
isalpha()Checks whether all characters are alphabetic (True or False).
isascii()Checks whether all characters are ASCII (True or False).
isdecimal()Checks whether all characters are decimal numbers (True or False).
isdigit()Checks whether all characters are digits, i.e., numbers from 0 to 9 (True or False).
isidentifier()Checks whether all characters are identifiers that can be used as names of functions, classes, or variables (True or False).
islower()Checks whether all characters are lowercase (True or False).
isnumeric()Checks whether all characters are numeric values (True or False).
isprintable()Checks whether all characters are printable (True or False).
isspace()Checks whether all characters are whitespaces (True or False).
istitle()Checks if the string is title-cased (True or False).
isupper()Checks whether all characters are uppercase (True or False).
join()Concatenates the elements in an iterable.
ljust()Returns a left-justified string filling up the right-hand side with fill characters.
lower()Returns a lowercase string version.
lstrip()Trims whitespaces on the left and returns a new string.
maketrans()Returns a translation table.
partition()Searches for a separator substring and returns a tuple with three strings: (1) everything before the separator, (2) the separator itself, and (3) everything after it.
removeprefix()Return string[len(prefix):] if the string starts with prefix, and string[:] otherwise.
removesuffix()Return string[:-len(suffix)] if the string starts with suffix, and string[:] otherwise.
replace()Returns a string with replaced values.
rfind()Return the highest index in the string where a substring is found. Returns -1 if not found.
rindex()Return the highest index in the string where a substring is found. Returns ValueError if not found.
rjust()Returns a right-justified string filling up the left-hand side with fill characters.
rpartition()Searches for a separator substring and returns a tuple with three strings: (1) everything before the separator, (2) the separator itself, and (3) everything after it.
rsplit()Splits the string at a given separator and returns a split list of substrings.
rstrip()Trims whitespaces on the right and returns a new string.
split()Splits the string at a given separator and returns a split list of substrings.
splitlines()Splits the string at line breaks such as 'n' and returns a split list of substrings (i.e., lines).
startswith()Returns whether the string starts with a given value or not (True or False).
strip()Trims whitespaces on the left and right and returns a new string.
swapcase()Swaps lowercase to uppercase characters and vice versa.
title()Returns a new string with uppercase first characters of each word.
translate()Returns a translated string.
upper()Returns a lowercase string version.
zfill()Fills the string from the left with "0" characters.

References