When you’re dealing with strings in Python, you may want to convert them to lowercase. This is a standard practice to ensure that your data is consistent and that you don’t hold, unwillingly, duplicate entries. In this tutorial, I’ll explain what are the different options you can use to lowercase strings in Python.
Table of Contents
Use lower() to lowercase a string
Luckily, Python comes with a built-in function, lower() that returns a copy of a string, replacing all cased characters by their lowercase equivalents. It’s worth noting that numbers and special characters, that are incompatible with this operation, are not removed from your string.
The syntax is pretty straightforward:
Code language: Python (python)string_lower = string.lower()
Let’s assume that you are asking an user its city of birth and that you want to store it lowercase. The code you’d need to execute is the following one:
place_of_birth = input('Where were you born?')
print(place_of_birth)
print(place_of_birth.lower())
Code language: Python (python)
Output:
LYON lyon
You simply take the input from your user and apply the built-in method lower(). If the string contains a number and/or a special characters, they are left unchanged as we explained before:
1st district of LYON 1st district of lyon
Use lower() to lowercase a list of strings
We often want to apply this modification to a list of strings, to ensure that we are not holding, unwillingly, duplicates. If you would like to apply this modification on a list of strings, you can use different approaches.
- lower() with a list comprehehension
list_strings = ['A','B','C']
list_strings_lower = [element.lower() for element in list_strings]
print(list_strings_lower)
Code language: Python (python)
Output:
['a', 'b', 'c']
Code language: Python (python)
- map() with a lambda function
The map() function is the easiest way, in Python, to apply a function to a list. We can leverage this functionality to achieve our objective:
list_strings = ['A','B','C']
list_strings_lower = list(map(lambda x: x.lower(), list_strings))
print(list_strings_lower)
Code language: Python (python)
Output:
['a', 'b', 'c']
Code language: Python (python)
How can I check if a string is lowercase?
To ensure that we don’t run useless code, we can apply this transformation only if our string is not already lowercase. Luckily for us, Python comes with another method, islower() that returns a boolean (either True or False) to indicate if all characters are lowercase.
Let’s confirm its behavior:
string_lower = 'i am lowercase'
string_not_lower = 'I am NOT'
print(string_lower.islower())
print(string_not_lower.islower())
Code language: Python (python)
Output:
True
False
Code language: Python (python)
Now, we can use this method to create a more efficient way to lowercase() a string only if it needs to.
#list of strings
strings = ['A','b','C']
#new list we'll be generating
new_strings = []
#variable to see how many times we use the method
n=0
#start loop
for string in strings:
#if string is not lower() already
if not string.islower():
#append string converted to lowercase
new_strings.append(string.lower())
n+=1
else:
#append the same string
new_strings.append(string)
print(new_strings)
print(f'The method has been used {n} times!')
Code language: Python (python)
Output:
['a', 'b', 'c']
The method has been used 2 times!
Code language: Python (python)
We can save on execution time by adding this simple if statement. If you are not handling a lot of data, you do not need to worry about it.
Use Pandas to lowercase a string
Finally, you can leverage the value_counts() method from the Pandas library. I recommend doing it like only if you are already working with a DataFrame. You can use the str.lower() or str.islower(), with similar behavior than the built-in methods.
import pandas as pd
df = {'strings':['A','B','C']}
df = pd.DataFrame(df, columns=['strings'])
df['strings'] = df['strings'].str.lower()
print(df)
Code language: Python (python)
Output:
a |
b |
c |