Python String lower() Method



The Python String lower() method converts all the case-based characters in a string into lowercased characters. However, for the strings containing only lowercased characters, it will not be converted and remains the same. This method is also exempt for strings containing number and symbol characters, as they are not case-based.

The lower() method is a counterpart for the upper() method - in which the letters will be uppercased as a result; and they are both applied in real-time, if we want to develop an application where case-sensitivity is not taken into consideration.

Syntax

Following is the syntax for Python String lower() method −

str.lower()

Parameters

This method does not accept any parameters.

Return Value

This method returns a copy of the string in which all case-based characters have been lowercased.

Example

The string taken as the input has all uppercase letters, so the method returns the lowercased string as the result.

The following example shows the usage of Python String lower() method. Here, we are passing an uppercased string value as a parameter to this method.

 
str = "THIS IS STRING EXAMPLE";
print(str.lower())

When we run above program, it produces following result −

this is string example

Example

If the input string taken has all lowercase letters, the method returns the original string as the result.

 
str = "this is a string";
print(str.lower())

The program above, on executing, displays the following result −

this is a string

Example

When we take a numbered string as an input, the method returns the original string as the result.

In the following example, we assign a numbered string to the variable str. Then, we call the lower() method on this string.

 
str = "287193778190";
print(str.lower())

On executing the above program, the output is displayed as follows −

287193778190

Example

When we pass a symbol string as the input, the result produced by the method is the original string itself.

In this example program, we are creating a string with only symbols and calling the lower() method on it. The method will not modify the string, as it does not contain case-based characters.

 
str = "@#$%^&*(";
print(str.lower())

When we run above program, the following result is obtained −

@#$%^&*(

Example

In a non case sensitive environment, two strings are compared to see if they are equal using the lower() method. The return value will be either true or false.

In this example program, we creating two strings converting them to lower case using this method. Then, using the conditional statements, we check if both strings are equal after they are lowercased. The program prints "Both strings are equal" if they are equal and "Both strings are not equal" otherwise.

 
str1 = "STRING EXAMPLE"
str2 = "STRING example"
if(str1.lower() == str2.lower()):
   print("Both strings are equal")
else:
   print("Both strings are not equal")

The output for the program above is displayed as −

Both strings are equal
python_strings.htm
Advertisements