Uppercase and Lowercase Strings in Python (Conversion and Checking)
In Python, the string type (str
) has methods for handling uppercase and lowercase characters. You can convert characters to different cases and check their case.
If you want to check if a string contains digits or letters, see the following article:
Convert between uppercase and lowercase
Basic usage
First, let's review the basic usage. This example uses the upper()
method to convert all characters to uppercase, but the other methods work similarly.
In Python, string objects (str
) are immutable; thus, the original string remains unmodified.
s_org = 'pYThon proGramminG laNguAge'
print(s_org.upper())
# PYTHON PROGRAMMING LANGUAGE
print(s_org)
# pYThon proGramminG laNguAge
If you want to use the converted string later, you can assign it to a new variable like this:
s_new = s_org.upper()
print(s_new)
# PYTHON PROGRAMMING LANGUAGE
You can also overwrite the original variable.
s_org = s_org.upper()
print(s_org)
# PYTHON PROGRAMMING LANGUAGE
Convert to uppercase: str.upper()
s_org = 'pYThon proGramminG laNguAge'
print(s_org.upper())
# PYTHON PROGRAMMING LANGUAGE
Convert to lowercase: str.lower()
s_org = 'pYThon proGramminG laNguAge'
print(s_org.lower())
# python programming language
Capitalize string: str.capitalize()
The capitalize()
method converts the first character of the string to uppercase and the rest to lowercase.
s_org = 'pYThon proGramminG laNguAge'
print(s_org.capitalize())
# Python programming language
Convert to title case: str.title()
The title()
method converts the first character of each word in the string to uppercase and the rest to lowercase.
s_org = 'pYThon proGramminG laNguAge'
print(s_org.title())
# Python Programming Language
Swap uppercase and lowercase letters: str.swapcase()
s_org = 'pYThon proGramminG laNguAge'
print(s_org.swapcase())
# PytHON PROgRAMMINg LAnGUaGE
Check uppercase and lowercase characters
In the following examples, methods are called directly on string literals, such as 'python'
, but they can also be used with strings stored in variables.
print('PYTHON'.isupper())
# True
print('Python'.isupper())
# False
Characters without case distinction
Characters without case distinctions, such as numbers and symbols, are ignored. If the string contains only these characters, the following methods return False
.
print('[123]'.isupper())
# False
Check if all characters are uppercase: str.isupper()
The isupper()
method returns True
if the string contains at least one alphabetic character, all of which are uppercase; otherwise, it returns False
.
print('PYTHON'.isupper())
# True
print('Python'.isupper())
# False
Check if all characters are lowercase: str.islower()
The islower()
method returns True
if the string contains at least one alphabetic character, all of which are lowercase; otherwise, it returns False
.
print('python'.islower())
# True
print('Python'.islower())
# False
Check if the string is title case: str.istitle()
The istitle()
method returns True
if the string is title case (i.e., the first character of each word is uppercase, and the rest are lowercase); otherwise, it returns False
.
print('Python Programming Language'.istitle())
# True
print('PYTHON Programming Language'.istitle())
# False
Note that the istitle()
method returns False
if a word starts with characters without case distinctions followed by lowercase letters.
print('The 1st Team'.istitle())
# False
print('The 1St Team'.istitle())
# True