How do you get a string to be all lowercase in Python? Thankfully there’s a built-in method that doesn’t require importing any external libraries to get a string to lower case, here’s how you do it.
To get a string to all lowercase, use the
built-in string method
.lower()
, which turns all characters in the string variable to lower case.
Here is an example demonstrating the transformation of a string containing upper cases and converting them all to lower cases:
>>> my_string = "WHY ARE YOU YELLING?"
>>> my_string.lower()
'why are you yelling?'
>>> print(my_string)
WHY ARE YOU YELLING?
As you can see from the first example above the initial string containing all upper case characters is converted to all lower case characters, but notice how running the method did not make a change to the original string.
When outputting the string using the
print()
built-in function it outputs the original string without being in lower case. Therefore, to capture the lower case version of the string you need to reference the result to another variable.
How Can You Make The Original String Lower Case?
So how can you convert the original string to be lower case? If you want to mutate the original string so that it permanently changes to lower case then you can reassign the variable back to the result, like so:
>>> my_string = "STOP YELLING!"
>>> my_string = my_string.lower()
>>> print(my_string)
stop yelling!
As you can see from the above example you can change the original string by re-assigning the same variable back to the result from the
.lower()
string method.
How To Know If Characters In String Are Lower Case?
Python also provides another
built-in string method called
.islower()
which checks to see if all characters in the string are lower case. If all characters in the string are lower case the method returns
True
, otherwise
False
.
Here is an example demonstrating how this method works:
>>> my_string = "am i too small?"
>>> my_string.islower()
True
>>> my_string.lower()
'am i too small?'
>>> my_string = "am I too small?"
>>> my_string.islower()
False
>>> my_string.lower()
'am i too small?'
As you can see from the above examples if all the characters in a string are lower case then the
.islower()
method returns
True
. This means by running the string method
.lower()
on the string
nothing will change
.
However, should there be just
one character
in the string which is upper case in nature then the method returns
False
. This means by running the string method
.lower()
on the string the output will be different compared to the original.
This method therefore can help to determine whether or not to use the
.lower()
method on your string.
Summary
The
.lower()
string method can help turn your string into all lower case characters provided there is a lower case option for the character. To change the original variable to be lower case simply reassign the original variable to the output of the
.lower()
method, like
my_string = my_string.lower()
.
To check if the string is already lower case use the other string method
.islower()
to see if converting all characters in the string need to be made lowercase.