Python Lowercase

By | September 28, 2021
Python Lowercase

In Python, a string object has many built-in methods which can also help in manipulating the case of the string.

The string .lower() method can convert all the uppercase characters of the string into a lower case and return the value. This method comes very useful when we want to check if two strings are the same despite their’s casing.

Python string .lower() method

The .lower() Method can only apply on the string variables or objects, and it returns a copy of the string characters all in lower case.

Syntax:

s.lower()

Example;

Vamware
>>> title = "TechGeekBuZZ"
>>> t = title.lower()
>>> print(t)

Output:

techgeekbuzz

Check if two strings are the same

The == operator return True if both the string has the equal value and similar casing. But if both the strings have the same value and different casing it returns False. This could be a problem when we do not want to consider casing. So to solve this kind of problem we can use the string .lower() method.

Vamware

Example

a = "TECHGeekBUZZ.com"
b = "techgeekbuzz.com"

if a.lower() == b.lower():
    print("Both the urls are same")
else:
    print("Both the urls are different")

Output

Both the urls are same

String .islower() method

The islower() is a string method is used to check if all the characters of the string are in lowercase or not.  This method returns True if all the characters in the string are in lower case. If the string contains even a single uppercase character, then this method returns False.

Example

>>> str1 = "Techgeekbuzz"
>>> str1.islower()
False

>>> str2 ="techgeekbuzz"
>>> str2.islower()
True

Python string upper() and isupper() methods

  • The upper() method works opposite of lower() method, it converts all the string character to uppercase character.
  • The isupper() method is used to check if all the characters in the string are in uppercase or not.

Example:

a = "Techgeekbuzz.com"

b= a.upper()
print(b)

Output

TECHGEEKBUZZ.COM

Summary

  • To convert all the characters of a string into lowercase, we use the .lower() method.
  • The islower() method check the lowercase character in a string.
  • The upper() method converts all the string characters into uppercase.
  • The lower() and upper() can be used to check if both the string are same despite of the characters casing.

People are also reading: 

Author: Vinay

I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.

Leave a Reply

Your email address will not be published.