Online Tutorials & Training Materials | STechies.com
Register Login

How to lowercase a String in Python?

24 Sep 2019 1:51 pm || 0

In this article, we will learn how to convert uppercase characters to lowercase. And also how to check if the string is in lowercase or not.

Python lower() Function

In Python, a built-in function lower() is available that converts uppercase string into lowercase string and returns it. 

In laymen language this method converts all the uppercase character present in string in lowercase character, if there is no uppercase character present in the given string it will return the original string.

Syntax

string.lower()

Parameters

Does not take any parameters 

Returns

Returns a lowercase string, if no lower case character present it return input string

Example

# Python code to explain lower() function 
  
# Initialize a string with all uppercase characters 
str1 = 'STECHIES.COM'
print(str1.lower())

# Initialize a string with uppercase & lowercase characters 
str1 = 'StecHies.com'
print(str1.lower())

Output

stechies.com
stechies.com

Explanation

In the above code, we initialized 2 strings with 2 different values. The first variable str1 holds a string in all uppercase characters. Whereas the second variable str2 holds a string with the only first character in uppercase. When we applied lower() function in both the variables. It converted the whole of str1 to lowercase. And only the first character of str2 to lowercase as rest of the string was already in lowercase. Thus we got the shown output.

Use of lower() Function in Application

Example

# Python program to compare two string using lower() function

# Initialize strings to compare
str1 = 'STECHIES'
str2 = 'SteChies'

# Compare string without using lower() function
if(str1 == str2):
    print(str1,'and ',str2,' are same')
else:
    print(str1,'and ',str2,' are not same')
    
# Compare string with lower() function
if(str1.lower() == str2.lower()):
    print(str1,'and ',str2,' are same')
else:
    print(str1,'and ',str2,' are not same')

Output

STECHIES and  SteChies  are not same
STECHIES and  SteChies  are same

Explanation

In the above example, we used if-else condition to check if the string are same or not. The first if-else condition gives the output “STECHIES and  SteChies are not same. This is because python is a case sensitive programming language so it treats capital “S” and small ‘s’ as two different characters.

In the second if-else condition we used lower() function to convert uppercase characters to lowercase. After using this function both of our string becomes same. Thud we got the output as “STECHIES and SteChies are same

islower() function

In python islower() is a build-in function, this function checks whether all the characters present in a string is lowercase or not.

Syntax

string.islower()

Parameters

Does not take any parameters 

Returns

  • True: if all characters are lowercase
  • False: if one or more characters are uppercase

Example

# Python program to check all characters in string is lowercase 

# Initialize strings
str1 = 'STECHIES'
str2 = 'SteChies'
str3 = 'stechies'

# Check if strings are in lowercase or not
print('String 1 STECHIES lower: ', str1.islower())
print('String 2 SteChies lower: ', str2.islower())
print('String 3 stechies lower: ', str3.islower())

Output

String 1 STECHIES lower:  False
String 2 SteChies lower:  False
String 3 stechies lower:  True

Explanation

In the above example, we used a built-in function islower() to check whether the given string is in lowercase or not. If the whole of the string is in lower case it returns True else it returns false.

Conclusion

To convert whole of the string into lowercase we use a built-in function lower(). This function converts every character in a string to lowercase. If the string is already in lowercase then the original string is returned. To check whether the string is in lowercase or not we have a built-in function islower()This function returns the boolean value Ii.e either true or false.