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;
>>> title = "TechGeekBuZZ"
>>> t = title.lower()
>>> print(t)
Output:
techgeekbuzz
How to 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.
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 oflower()
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:
- Slice Lists/Arrays and Tuples in Python
- Invalid Syntax Python
- Python Absolute Value
- How to Sort a Dictionary in Python?
- Check Python Version
- Remove Duplicates from a Python List
- Substring a String in Python
- How to Iterate through a Dictionary in Python?
- Python Reverse List
- Not Equal Operator in Python
Leave a Comment on this Post