Sunday, November 14, 2021

ItsMyCode: Python String lower()

ItsMyCode |

Python String lower() method converts all the uppercase characters in a string to lowercase characters and returns the string as output.

lower() Syntax

The Syntax of lower() method is:

string.lower()

lower() Parameters

The lower() method doesn’t take any parameters.

lower() Return Value

In Python string lower() method converts all the uppercase characters in a string to lowercase and returns the output of the string.

The lower() method will return the original string if the string doesn’t have any uppercase.

Example 1: Convert a string to lowercase

The following program converts all the uppercase characters in a string to lowercase.

# Program to convert uppercase characters to lowercase
text= "ItsMYCode Coding Simplified"
text2="HELLO WORLD"
print('Original String: ',text)
print('Original String: ',text2)

print('Lowercase String: ',text.lower())
print("Lowercase String: ", text2.lower())

Output

Original String:  ItsMYCode Coding Simplified
Original String:  HELLO WORLD
Lowercase String:  itsmycode coding simplified
Lowercase String:  hello world

Example 2: String with Alphanumeric characters

If the string contains numeric/digits, the lower() method will return as is. Only the characters with uppercase will be converted to lowercase and returned.

Note: The same logic applies to even symbols and special characters. Apart from uppercase, none of the other characters will be converted. It will be returned as-is.

# Program to convert Alphanumeric characters to lowercase
text= "Its 2:00PM IST in India"
text2="HELLO WORLD"
print('Original String: ',text)
print('Lowercase String: ',text.lower())

Output

Original String:  Its 2:00PM IST in India
Lowercase String:  its 2:00pm ist in india

Example 3: How to check if two strings are same in Python

Let’s take a look at real-time examples on when exactly we use the string lower() method. Generally, there are many use cases where we need to check if two strings are the same.


In the below use case, we check if the email address and the re-entered email address are the same or different.

# Program to check the email and re-enterd email is same or different
email= "info@itsmycode.com"
confirmemail="INFO@ITSMYCODE.COM"

if(email.lower() == confirmemail.lower()):
    print("Email Address are same")
else:
    print("Email Address are not same")

Output

Email Address are same

The post Python String lower() appeared first on ItsMyCode.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...