To check if all characters in a string are lowercase, we can use the Python built-in string islower() function.
string_1 = "This is a String with SOME letters."
string_2 = "hello"
print(islower(string_1))
print(islower(string_2))
#Output:
False
True
When working with strings in Python, being able to get information about your variables easily is important. There are a number of built in string methods which allow us to get information and change string variables.
One such function which is very useful is the string islower() function. With the islower() function, we can determine if a string has all lowercase letters.
If the string has all lowercase letters, islower() returns “True”. Otherwise, if there is at least one capital letter, islower() returns “False”.
Below are a few examples of how to use the Python islower() function.
string_1 = "This is a String with SOME letters."
string_2 = "hello"
print(islower(string_1))
print(islower(string_2))
#Output:
False
True
Converting a String to Lowercase with the String lower() Python Function
Let’s say you are working a program where you need your inputs to be lowercase. Another useful string method related to the islower() Python function is the lower() function.
The lower() function in Python converts all letters of a string to lowercase.
Let’s see how the Python lower() function works with the example strings from above.
string_1 = "This is a String with SOME letters."
string_2 = "hello"
print(string_1.lower())
print(string_2.lower())
#Output:
this is a string with some letters.
hello
As you can see, now all of the letters are lowercase. If we pass these strings to islower(), both checks will come back “True”.
string_1 = "This is a String with SOME letters."
string_2 = "hello"
print(islower(string_1.lower()))
print(islower(string_2.lower()))
#Output:
True
True
Hopefully this article has been useful for you to understand how to check if all letters in a string are lowercase in your Python code.