List of string methods for manipulating uppercase and lowercase letters in Python

Money and Business

Python's string type (str) comes standard with convenient methods for manipulating uppercase and lowercase letters. You can convert between uppercase and lowercase and determine the case.

The following information is provided here.

  • Converting between uppercase and lowercase letters
    • Basic usage
    • Handling of full-size and half-size characters
    • str.upper()Convert all letters to uppercase
    • str.lower()Convert all letters to lowercase
    • str.capitalize()Convert the first letter to uppercase and the rest to lowercase.
    • str.title()Convert the first letter of a word to uppercase and the rest to lowercase.
    • str.swapcase()Convert uppercase letters to lowercase and lowercase letters to uppercase.
  • Determine uppercase and lowercase letters
    • str.isupper(): Determine if all letters are uppercase
    • str.islower(): Determine if all characters are lowercase.
    • str.istitle(): Determine if it is a title case.
  • Compare strings in a case-insensitive manner

Converting between uppercase and lowercase letters

Basic usage

First, I will explain the basic usage. We will use the upper() method to capitalize all letters as an example, but the same applies to other methods.

For the sake of convenience, we write “conversion”, but in Python, string type (str) objects are not updatable, so the original string (s_org in the example) itself is not changed.

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 store it in a new variable as follows.

s_new = s_org.upper()
print(s_new)
# PYTHON PROGRAMMING LANGUAGE

It is also possible to overwrite the original variable.

s_org = s_org.upper()
print(s_org)
# PYTHON PROGRAMMING LANGUAGE

Handling of full-size and half-size characters

If a character is case-sensitive, such as the alphabet, it will be converted to both single-byte and double-byte characters.

Characters that are not case-sensitive, such as numbers and Chinese characters, remain unchanged. The example is for upper(), but the same applies to other methods.

s_org = 'Pyhon Python 123'

print(s_org.upper())
# PYHON PYTHON 123

str.upper(): convert all letters to uppercase

s_org = 'pYThon proGramminG laNguAge'

print(s_org.upper())
# PYTHON PROGRAMMING LANGUAGE

str.lower(): convert all characters to lowercase

s_org = 'pYThon proGramminG laNguAge'

print(s_org.lower())
# python programming language

str.capitalize(): convert the first letter to uppercase, the rest to lowercase

s_org = 'pYThon proGramminG laNguAge'

print(s_org.capitalize())
# Python programming language

str.title(): convert the first letter of a word to uppercase and the rest to lowercase

Conversion to a so-called title case.

s_org = 'pYThon proGramminG laNguAge'

print(s_org.title())
# Python Programming Language

str.swapcase(): convert uppercase to lowercase, lowercase to uppercase

Swap uppercase and lowercase letters.

s_org = 'pYThon proGramminG laNguAge'

print(s_org.swapcase())
# PytHON PROgRAMMINg LAnGUaGE

Determine uppercase and lowercase letters

In the following examples, methods are called directly from string literals such as 'python', but the same is true when stored in variables as in the previous examples.

str.isupper(): determine if all letters are uppercase

isupper() returns true if it contains at least one case-sensitive character and all of them are uppercase, and false otherwise.

print('PYTHON'.isupper())
# True

print('Python'.isupper())
# False

If the character is case-sensitive, even double-byte characters are judged.

print('PYTHON'.isupper())
# True

If even a single case-sensitive character is included, the case-insensitive character is ignored, but if no case-sensitive character is included (all characters are case-insensitive), the decision is false.

print('PYTHON 123'.isupper())
# True

print('123'.isupper())
# False

str.islower(): determine if all characters are lowercase

islower() returns true if it contains at least one case-sensitive character and all of them are lowercase, and false otherwise.

print('python'.islower())
# True

print('Python'.islower())
# False

If the character is case-sensitive, even double-byte characters are judged.

print('python'.islower())
# True

If even a single case-sensitive character is included, the case-insensitive character is ignored, but if no case-sensitive character is included (all characters are case-insensitive), the decision is false.

print('python 123'.islower())
# True

print('123'.islower())
# False

str.istitle(): Determine if the case is a title case.

istitle() returns true if the string is a title case (the first letter of the word is uppercase, the rest are lowercase), false otherwise.

print('Python Programming Language'.istitle())
# True

print('PYTHON Programming Language'.istitle())
# False

If it contains case-insensitive characters, it will be false if the case-insensitive characters are preceded by a lowercase letter.

print('★Python Programming Language'.istitle())
# True

print('Python★ Programming Language'.istitle())
# True

print('Py★thon Programming Language'.istitle())
# False

Note that there are not many strings like the example above, but it is realistic to include numbers in ordinal numbers and other cases.

print('The 1st Team'.istitle())
# False

print('The 1St Team'.istitle())
# True

If no case-sensitive characters are included (all characters are case-insensitive), false.

print('123'.istitle())
# False

Compare strings in a case-insensitive manner

When comparing strings, different uppercase and lowercase letters are not considered equivalent.

s1 = 'python'
s2 = 'PYTHON'

print(s1 == s2)
# False

If you want to make a case-insensitive comparison, you can use upper() or lower() to convert the two and compare them.

print(s1.upper() == s2.upper())
# True

print(s1.lower() == s2.lower())
# True

print(s1.capitalize() == s2.capitalize())
# True

print(s1.title() == s2.title())
# True
Copied title and URL