Python offers five different methods that can be used to change or alter the case of a Python String.
They are upper(), lower(), swapcase(), title(), capitalize() methods.
1. string.upper()
upper() method in Python Strings can be used to convert all the characters of the given strings to uppercase (capital) letters.
Syntax
string.upper()
s = "Python loves me. I love Python."
print(s.upper())
O/P
PYTHON LOVES ME. I LOVE PYTHON.
string.isupper()
The isupper() method in Python String can be used to check if a given string has all upper case character or not. This method returns true if the string has all characters in uppercase.
s = "PY"
print(s.isupper())
O/P
True
2. string.lower()
lower() method in Python Strings can be used to convert all the characters of the given strings to lowercase(small) letters.
Syntax
string.lower()
s = "Python loves me. I love Python."
print(s.lower())
O/P
python loves me. i love python.
string.islower()
The islower() method in Python String can be used to check if a given string has all lower case character or not. This method returns true if the string has all characters in uppercase.
s = "py"
print(s.islower())
O/P
True
3. string.title()
title() method in Python Strings can be used to convert a string in a way such that first character of a string is always in uppercase and other characters are in lowercase.
Syntax
string.title()
s = "he is my friend."
print(s.title())
O/P
He Is My Friend.
string.istitle()
The istitle() method in Python String returns True if each word of the string starts with capital letter and there is atleast one character in the string, else it returns False.
s = "He Is My Friend."
print(s.istitle())
O/P
True
4. string.swapcase()
swapcase() method in Python Strings converts all :
lowercase characters —> uppercase characters and vice versa.
Syntax
string.swapcase()
s = "He Is My Friend."
print(s.swapcase())
O/P
hE iS mY fRIEND.
5. string.capitalize()
capatalize() method in Python String converts first character of string to uppercase and rest of them to lowercase letters.
Syntax
string.capitalize()
s = "He Is My Friend."
print(s.capitalize())
O/P
He is my Friend.
Few other String Testing methods
1. isalnum(): This method returns True if all the characters in the string are alpha-numeric (A to Z, a to z, 0 to 9) and there is atleast one character, else returns False.
s = "Bug4coding"
print(s.isalnum())
O/P
True
2. isalpha(): This method returns True if all the characters in the string are alphabetic (A to Z, a to z) and there is atleast one character, else returns False.
s = "Bugcoding"
print(s.isalpha())
O/P
True
3. isdigit(): This method returns True if it contains only numeric digits, else returns False.
s = "12324"
print(s.isdigit())
O/P
True
Well, that’s all.
That’s all here.
