How to Convert and Check String Case in Python (upper, lower, isupper, islower)
Hello, this is BlockDMask.
Today, we’ll cover how to convert strings to uppercase or lowercase in Python, as well as how to check if a string is uppercase or lowercase.
1. Convert string to uppercase - upper()
method
The upper()
method converts all alphabetic characters in the string to uppercase. The original string is not modified; it returns a new string.
Basic syntax:
string.upper()
# string: the target string object
# return: new string converted to uppercase
Examples & Results:
# Example 1
name = 'BlockDMask'
upper_name = name.upper()
print('name :', name)
print('upper_name :', upper_name)
# Output:
# name : BlockDMask
# upper_name : BLOCKDMASK
→ The original string remains unchanged; a new uppercase string is returned.
# Example 2
word = 'PyThOnIsFun'.upper()
print('word :', word)
# Output:
# word : PYTHONISFUN
→ Mixed-case letters are all converted to uppercase.
# Example 3 (reassigning to same variable)
city = 'new york'
city = city.upper()
print('city :', city)
# Output:
# city : NEW YORK
→ The variable is reassigned with the uppercase value.
# Example 4 (including numbers and symbols)
mixed = 'abc 123,./blog'
print('mixed :', mixed.upper())
# Output:
# mixed : ABC 123,./BLOG
→ Only alphabetic characters are converted; numbers and symbols remain unchanged.
※ Only alphabetic characters are converted. Numbers, symbols, and non-Latin characters remain unchanged.
2. Convert string to lowercase - lower()
method
The lower()
method converts all alphabetic characters in the string to lowercase. It also returns a new string without modifying the original.
Basic syntax:
string.lower()
# string: the target string object
# return: new string converted to lowercase
Examples & Results:
# Example 1
brand = 'BlockDMask'
lower_brand = brand.lower()
print('brand :', brand)
print('lower_brand :', lower_brand)
# Output:
# brand : BlockDMask
# lower_brand : blockdmask
→ The mixed-case string has been fully converted to lowercase.
# Example 2
sentence = 'WELCOME TO PYTHON WORLD.'.lower()
print('sentence :', sentence)
# Output:
# sentence : welcome to python world.
→ Only the English letters are converted; non-alphabet characters stay the same.
# Example 3 (numbers and symbols included)
sample = 'HELLO123@BLOG456'
print('sample :', sample.lower())
# Output:
# sample : hello123@blog456
→ Numbers and special characters remain unaffected while letters convert to lowercase.
3. Check if string is uppercase - isupper()
method
The isupper()
method checks if all alphabetic characters in the string are uppercase. If any lowercase letter exists, or if there are only non-letter characters, it returns False
.
Basic syntax:
string.isupper()
# return: Bool (True or False)
Examples & Results:
# Example 1
company = 'BlockDMask'
upper_company = company.upper()
print(f"{company} is uppercase? : {company.isupper()}")
print(f"{upper_company} is uppercase? : {upper_company.isupper()}")
# Output:
# BlockDMask is uppercase? : False
# BLOCKDMASK is uppercase? : True
→ Returns False if mixed, True if fully uppercase.
# Example 2
test_string = 'HeLLo Python!'
for char in test_string:
print(f"'{char}' is uppercase? : {char.isupper()}")
# Output example:
# 'H' is uppercase? : True
# 'e' is uppercase? : False
# 'L' is uppercase? : True
# 'l' is uppercase? : False
# 'o' is uppercase? : False
# ' ' is uppercase? : False
# 'P' is uppercase? : True
# 'y' is uppercase? : False
# 't' is uppercase? : False
# 'h' is uppercase? : False
# 'o' is uppercase? : False
# 'n' is uppercase? : False
# '!' is uppercase? : False
→ Only uppercase alphabetic characters return True; others return False.
4. Check if string is lowercase - islower()
method
The islower()
method checks if all alphabetic characters in the string are lowercase.
If so, it returns True
. Otherwise, it returns False
.
Basic syntax:
string.islower()
# return: Bool (True or False)
Examples & Results:
# Example 1
language = 'Python'
lower_language = language.lower()
print(f"{language} is lowercase? : {language.islower()}")
print(f"{lower_language} is lowercase? : {lower_language.islower()}")
# Output:
# Python is lowercase? : False
# python is lowercase? : True
→ Returns False when any uppercase letters exist; True if fully lowercase.
# Example 2
combo = 'a B 2 $%'
for char in combo:
print(f"'{char}' is lowercase? : {char.islower()}")
# Output example:
# 'a' is lowercase? : True
# ' ' is lowercase? : False
# 'B' is lowercase? : False
# '2' is lowercase? : False
# ' ' is lowercase? : False
# '$' is lowercase? : False
# '%' is lowercase? : False
→ Only lowercase alphabetic characters return True; others return False.
That's it for how to convert and check string cases in Python.
Thank you for reading!
Comments
Post a Comment