Python Lowercase String with .lower(), .casefold(), and .islower()

Python Lowercase a String Cover Image

Learn how to use Python to lowercase text using both the str.lower() and str.casefolds() methods.

By the end of this tutorial, you’ll learn when to use both methods, including forcing special characters such as ß to their lower case alternatives. You’ll also learn how to check if a string is already lowercase by using the str.islower() method and how to convert lists of strings to their lower case strings. You’ll also learn how to convert a Pandas dataframe column to lowercase.

Being able to work with strings is an incredibly useful skill to learn for any budding Pythonista or data scientist. Data science is rapidly moving more and more into text analysis, which requires text to be pre-processed. One of the many transformations you’ll often undertake is to convert text to its lowercase equivalent.

Knowing how to do this and how to check if strings are already lower case is an important skill to learn. Let’s get started!

The Quick Answer: Use .lower() and .casefold()

Quick Answer - Python Lowercase String

Python Lowercase String with lower

Python strings have a number of unique methods that can be applied to them. One of them, str.lower(), can take a Python string and return its lowercase version. The method will convert all uppercase characters to lowercase, not affecting special characters or numbers.

Python strings are immutable, meaning that the can’t be changed. In order to change a string to lowercase in Python, you need to either re-assign it to itself or to a new variable.

Let’s take a look at an example of how we can use the Python str.lower() method to change a string to lowercase:

# Convert a String to Lowercase in Python with str.lower()

a_string = 'LeArN how to LOWER case with datagy'
lower = a_string.lower()

print(lower)

# Returns: learn how to lower case with datagy

You could also re-assign the string to itself to prevent having to create a new variable. Let’s see how this would work:

# Convert a String to Lowercase in Python with str.lower()

a_string = 'LeArN how to LOWER case with datagy'
a_string = a_string.lower()

print(a_string)

# Returns: learn how to lower case with datagy

In the next section, you’ll learn about a similar string method, str.casefold(), which also helps convert special characters to lowercase in Python.

Need to check if a key exists in a Python dictionary? Check out this tutorial, which teaches you five different ways of seeing if a key exists in a Python dictionary, including how to return a default value.

Python Lowercase String with casefold

Beginning in Python 3.3, Python introduced a new string method to lowercase text, str.casefold(). What makes str.casefold() different from str.lower() is that it uses aggressive case lowering.

While on the surface the two methods will yield nearly identical results, the str.casefold() method will also return “lowercase” versions of special characters. For example, the letter ß will remain the same when the str.lower() method is used. However, the str.casefold() method will return ss.

Let’s see how we can use str.casefold() method to lowercase a Python string:

# Convert a String to Lowercase in Python with str.casefold()

a_string = 'LeArN how to LOWER case with datagy'
a_string = a_string.casefold()

print(a_string)

# Returns: learn how to lower case with datagy

Finally, let’s compare the use of the lower() and casefold methods() with a practical example:

# Comparing Lowercasing Strings with .lower() and .casefold()

a_string = 'LowerCase Strings WITH datagy and ßome special characters'

lower = a_string.lower()
casefold = a_string.casefold()

print(f'{lower=}')
print(f'{casefold=}')

# Returns:
# lower='lowercase strings with datagy and ßome special characters'
# casefold='lowercase strings with datagy and ssome special characters'

We can see here that the str.lower() method kept the ß character, while the str.casefold() method converted it to ss.

In the next section of this tutorial, you’ll learn how to check if a Python string is completely lowercase or not.

Want to learn more about calculating the square root in Python? Check out my tutorial here, which will teach you different ways of calculating the square root, both without Python functions and with the help of functions.

Check if a Python String is Lowercase with islower

Python makes it very easy to see if a string is already lowercase, using the str.islower() method. The method will return a boolean value:

  • True is the entire string is lowercase, and
  • False is the entire string isn’t a lowercase

If there is even a single element in the string that isn’t lower cased, the method will return False.

Let’s see how we can use Python to check if a string is lowercased:

# Checking if a string is lower case in Python

a_string = 'LowerCase Strings WITH datagy'
lowered_string = a_string.lower()

# Check for lower case using .islower()
print(a_string.islower())
print(lowered_string.islower())

# Returns:
# False
# True

In the next section, you’ll learn how to turn a list of strings to lowercase.

Want to learn how to use the Python zip() function to iterate over two lists? This tutorial teaches you exactly what the zip() function does and shows you some creative ways to use the function.

Convert a List of Strings to Lowercase

You may find yourself with a Python list of strings that you need to convert to lowercase.

We can do this with either the str.lower() or str.casefold() methods. We’ll loop over each item in the list and turn it to lower case.

We’ll first go over how to accomplish this using a Python for loop:

# Convert a Python list of strings to lowercase with a for loop

a_list = ['DaTAGY', 'iS', 'a', 'PLAce', 'to', 'LEArn', 'PythoN']
lowered = []

for item in a_list:
    lowered.append(item.lower())

print(lowered)

# Returns: ['datagy', 'is', 'a', 'place', 'to', 'learn', 'python']

In order to make this happen, we first need to instantiate a new, empty list to hold our lowercased strings. We then loop over each string in the original list and append its lowered version to the new list.

Want to learn more about Python for-loops? Check out my in-depth tutorial that takes your from beginner to advanced for-loops user! Want to watch a video instead? Check out my YouTube tutorial here.

Now let’s take a look at how we can simplify this process by using a Python list comprehension:

# Convert a Python list of strings to lowercase with a list comprehension

a_list = ['DaTAGY', 'iS', 'a', 'PLAce', 'to', 'LEArn', 'PythoN']
lowered = [item.lower() for item in a_list]

print(lowered)

# Returns: ['datagy', 'is', 'a', 'place', 'to', 'learn', 'python']

While this does essentially the same thing as the for loop, it does save us some lines of code and doesn’t require us to for initialize an empty list.

In the next section, you’ll learn how to use string methods to convert a Pandas column to lowercase.

Want to learn more about Python list comprehensions? Check out this in-depth tutorial that covers off everything you need to know, with hands-on examples. More of a visual learner, check out my YouTube tutorial here.

Convert a Pandas Dataframe Column to Lowercase

Being able to work with string data in Pandas is an important skill. There are many times when you’ll encounter string data in tabular forms and you’ll need to convert it all to lower case.

Let’s load a Pandas dataframe to start off:

# Loading a Sample Pandas Dataframe
import pandas as pd
df = pd.DataFrame.from_dict({
    'Numbers': [1,2,3,4,5,6,7],
    'SomeStrings': ['DaTAGY', 'iS', 'a', 'PLAce', 'to', 'LEArn', 'PythoN'],
}) 

print(df)

# This returns:
#   SomeStrings  Numbers
# 0      DaTAGY        1
# 1          iS        2
# 2           a        3
# 3       PLAce        4
# 4          to        5
# 5       LEArn        6
# 6      PythoN        7

Now that we have a dataframe, let’s see how we can lowercase a column using Pandas string methods:

# Converting a Pandas column to Lower Case
import pandas as pd
df = pd.DataFrame.from_dict({
    'Numbers': [1,2,3,4,5,6,7],
    'SomeStrings': ['DaTAGY', 'iS', 'a', 'PLAce', 'to', 'LEArn', 'PythoN'],
}) 

df['SomeStrings'] = df['SomeStrings'].str.lower()

print(df)

# This returns:
#    Numbers SomeStrings
# 0        1      datagy
# 1        2          is
# 2        3           a
# 3        4       place
# 4        5          to
# 5        6       learn
# 6        7      python

You can see here that the entire column’s text has been converted to lowercase. We accomplished this by using the str.lower() method to it, which helps access the string portion of the column.

Want to learn more about Python f-strings? Check out my in-depth tutorial, which includes a step-by-step video to master Python f-strings!

Conclusion

In this tutorial, you learned how to use Python to convert strings to lowercase, using the str.lower() and the str.casefold() string methods. You also learned how to check if a string is already lowercase by using the str.islower() method. Finally, you learned how to convert a list of strings as well as a Pandas dataframe column to lowercase.

To learn more about the str.lower() method, check out the official documentation here.

To learn more about the str.casefold() method, check out the official documentation here.