Skip to main content

Lowercase in Python Tutorial

Learn to convert spreadsheet table column into lowercase using .str.lower().
Updated Nov 23, 2024  · 6 min read

.lower() is a built-in Python method primarily used for string handling. The .lower() method takes no arguments and returns the lowercased strings from the given string by converting each uppercase character to lowercase. If there are no uppercase characters in the given string, it returns the original string.

In the context of pandas, the .str.lower() method is a specialized function designed to operate on pandas Series (like columns in a DataFrame). It applies the .lower() functionality to each string element in the Series, making it an essential tool for standardizing text data.

Practice Case Conversion in Python with this hands-on exercise.

Using str.lower() in Pandas

When working with pandas DataFrames, the str.lower() method is often applied to an entire column of strings to standardize text formatting. Let’s look at an example to see how it works.

Here’s a sample dataset representing the prices of different fruits:

import pandas as pd

# Create a DataFrame
fruit_price = pd.DataFrame({
    'name': [
        'Apple', 'Banana', 'Orange', 'Watermelon',
        'Plum', 'Blueberries', 'Dragonfruit', 'Kiwi'
    ],
    'price_usd': [0.88, 0.23, 0.68, 3.98, 0.96, 5.16, 5.27, 1.12]
})
            name      price_usd
0          Apple           0.88
1         Banana           0.23
2         Orange           0.68
3     Watermelon           3.98
4           Plum           0.96
5    Blueberries           5.16
6    Dragonfruit           5.27
7           Kiwi           1.12 

We can apply the str.lower() method to convert all entries in the name column to lowercase. Here’s how it’s done:

fruit_price['name'] = fruit_price['name'].str.lower()
print(fruit_price)

Here's the output:

            name      price_usd
0          apple           0.88
1         banana           0.23
2         orange           0.68
3     watermelon           3.98
4           plum           0.96
5    blueberries           5.16
6    dragonfruit           5.27
7           kiwi           1.12

As you can see, all entries in the name column are now standardized to lowercase.

Learn Python From Scratch

Master Python for data science and gain in-demand skills.
Start Learning for Free

Interactive Example: Changing Case in a Movie Dataset

Let’s explore another example. Imagine we’re working with a dataset of movies. Each row in the dataset represents a movie, with columns for the title (movie_title), genre (movie_genre), and duration (movie_length). Here’s the initial dataset:

import pandas as pd

# Create a DataFrame
movies = pd.DataFrame({
    'movie_title': [
        'The Sumif All Fears', 'The Seaborn Identity', 'The Matrices',
        "There's Something About Merging", 'Mamma Median!',
        'Harry Plotter', 'Kung Fu Pandas', 'While You Were Sorting',
        '10 Things I Hate About UNIX'
    ],
    'movie_genre': [
        'Action', 'Action', 'Action', 'Comedy', 'Comedy',
        'Kids', 'Kids', 'Romance', 'Romance'
    ],
    'movie_length': [124, 119, 136, 119, 108, 159, 92, 103, 97]
})

We want to standardize the text in the movie_title column by converting all titles to lowercase. Here’s how we can achieve this using the .str.lower() method:

# Change movie_title column to lowercase
movies['movie_title'] = movies['movie_title'].str.lower()

# Look at the change
print(movies)

When you run the above code, it produces the following result:

                        movie_title movie_genre  movie_length
0              the sumif all fears      Action           124
1             the seaborn identity      Action           119
2                     the matrices      Action           136
3  there's something about merging      Comedy           119
4                    mamma median!      Comedy           108
5                    harry plotter        Kids           159
6                   kung fu pandas        Kids            92
7           while you were sorting     Romance           103
8      10 things i hate about unix     Romance            97

By applying .str.lower(), we ensure that the movie_title column is uniformly formatted, which is useful for tasks like filtering, comparisons, or performing case-insensitive operations.

Try it for yourself!

To learn more about putting pandas dataframe together in Python, please see this video from our course Python for Spreadsheet Users.

This content is taken from DataCamp's Python for Spreadsheet Users course by Chris Cardillo.

Get certified in your dream Data Analyst role

Our certification programs help you stand out and prove your skills are job-ready to potential employers.

Get Your Certification
Timeline mobile.png

Lowercase in Python FAQS

What is lowercase in Python?

It is a letter case when all the letters of a string are small. The opposite to lowercase is uppercase when all the letters of a string are capitals. Otherwise, if there are both small and capital letters in a string, it is called a mixed case.

How to make a string lowercase in Python?

Using a Python built-in string method lower() on the string or a variable representing it. For example, 'PythoN'.lower() will return 'python'. This method doesn't take in any arguments.

How to make all the strings in a Python list lowercase?

Using list comprehension. For example, [x.lower() for x in ['Python', 'R', 'SQL']] will return ['python', 'r', 'sql']. An important note here is that all the items in the list have to be of string type, otherwise the program will throw an error.

How to make all the strings in a Python Series lowercase?

It is possible to use list comprehension also on a Series ([x.lower() for x in pd.Series(['Python', 'R', 'SQL'])]), but a better approach here is to use the Series.str.lower() method. For example, pd.Series(['Python', 'R', 'SQL']).str.lower() will return:

0    python
1         r
2       sql
dtype: object

An important note here is that all the values of the Series object have to be of string type and no missing values are acceptable, otherwise the program will throw an error.

How to check if all the letters in a Python string are lowercase?

Using a Python built-in string method islower() on the string or a variable representing it. For example, 'Python'.islower() will return False since there is one uppercase letter in the string while 'python'.islower() will return True.

What type of data do the lower() and islower() methods return?

The lower() method returns a string while the islower() method returns a boolean.

Do the lower() and islower() methods work on numbers?

No. Applying these methods on numbers (e.g., 1.lower()) or any other data types that are not string will throw an error.

What happens if we apply the lower() or islower() methods on a string containing digits, white spaces, or special characters?

The lower() method leaves such characters as they are, the islower() ignore them. For example, 'Python1'.lower() will return 'python1', 'Python & R'.lower() will return 'python & r', and 'python & r'.islower() will return True. It works even for the strings containing emoji: 'I ❤️ Python'.lower() will return 'i ❤️ python'.

What are the opposite methods to lower() and islower() to make a string uppercase?

upper() and isupper(). The syntax and use cases of these methods are identical to lower() and islower(), they also work only on the string data type and ignore digits, white spaces, or special characters inside a string. To make all the strings in a Python Series uppercase, use the Series.str.upper() method.

Topics

Python Courses

course

Python for Spreadsheet Users

4 hr
27.3K
Use your knowledge of common spreadsheet functions and techniques to explore Python!
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

tutorial

How to Drop Columns in Pandas Tutorial

Learn how to drop columns in a pandas DataFrame.
DataCamp Team's photo

DataCamp Team

3 min

tutorial

Pandas Drop Duplicates Tutorial

Learn how to drop duplicates in Python using pandas.
DataCamp Team's photo

DataCamp Team

4 min

tutorial

Pandas Sort Values: A Complete How-To

Use sort_values() to reorder rows by column values. Apply sort_index() to rearrange rows by the DataFrame’s index. Combine both methods to explore your data from different angles.
DataCamp Team's photo

DataCamp Team

4 min

tutorial

Python String format() Tutorial

Learn about string formatting in Python.
DataCamp Team's photo

DataCamp Team

5 min

tutorial

Role of Underscore(_) in Python Tutorial

In this tutorial, you're going to learn about the uses of underscore(_) in python.
Hafeezul Kareem Shaik's photo

Hafeezul Kareem Shaik

8 min

tutorial

Python String Replace Tutorial

Learn to find and replace strings using regular expressions in Python.
DataCamp Team's photo

DataCamp Team

2 min

See MoreSee More