Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. str.upper() / str.lower() / str.title()

str.upper() / str.lower() / str.title()

Methods for converting the case of a string. Commonly used to normalize user input or preprocess strings before comparison.

Syntax

# Converts all characters in the string to uppercase.
str.upper()

# Converts all characters in the string to lowercase.
str.lower()

# Converts the first character to uppercase and the rest to lowercase.
str.capitalize()

# Converts the first character of each word to uppercase and the rest to lowercase (title case).
str.title()

# Swaps uppercase characters to lowercase and lowercase characters to uppercase.
str.swapcase()

Method List

MethodDescription
str.upper()Returns a new string with all lowercase letters converted to uppercase.
str.lower()Returns a new string with all uppercase letters converted to lowercase.
str.capitalize()Returns a new string with the first character uppercased and the rest lowercased.
str.title()Returns a new string with the first character of each word uppercased and the rest lowercased.
str.swapcase()Returns a new string with uppercase characters converted to lowercase and vice versa.

Sample Code

# Basic usage of upper() and lower().
text = "Hello, Python World!"
print(text.upper())  # HELLO, PYTHON WORLD!
print(text.lower())  # hello, python world!

# Compare strings ignoring case.
user_input = "Yes"
if user_input.lower() == "yes":
    print("Approved.")

# Case-insensitive command matching (hashing would be required for passwords).
command = "QUIT"
if command.upper() in ["QUIT", "EXIT", "Q"]:
    print("Exiting.")

# Basic usage of capitalize().
s1 = "hello world"
print(s1.capitalize())  # Hello world (only the first character is uppercased)

s2 = "HELLO WORLD"
print(s2.capitalize())  # Hello world (the rest are all lowercased)

# Convert to title case using title().
book_title = "the quick brown fox"
print(book_title.title())  # The Quick Brown Fox

# Note: title() also uppercases the character after an apostrophe.
name = "it's a wonderful life"
print(name.title())  # It'S A Wonderful Life ('S is uppercased)

# Swap case using swapcase().
text2 = "Hello World"
print(text2.swapcase())  # hELLO wORLD

# Case-checking methods are also useful.
print("HELLO".isupper())    # True (all uppercase)
print("hello".islower())    # True (all lowercase)
print("Hello World".istitle())  # True (title case)

# Practical example: normalize CSV headers.
headers = ["  NAME  ", "AGE", "city", "EMAIL ADDRESS"]
normalized = [h.strip().lower().replace(" ", "_") for h in headers]
print(normalized)  # ['name', 'age', 'city', 'email_address']

# Normalize a username.
username = "  Taro_YAMADA  "
normalized_name = username.strip().lower()
print(normalized_name)  # taro_yamada

Notes

All of these methods return a new string. The original string is not modified (strings are immutable). Case conversion only affects ASCII letters — digits, symbols, and non-Latin characters are not changed.

To compare strings in a case-insensitive way, the common approach is to convert both strings to lowercase with lower() before comparing. For stricter comparisons involving special characters such as the German 'ß', the casefold() method is recommended. casefold() applies more aggressive lowercasing than lower() and is better suited for internationalized comparisons.

For string formatting (such as number formatting or padding), see 'str.format() / f-strings'. To check whether a string starts with a specific pattern, see 'str.startswith() / str.endswith()'.

If you find any errors or copyright issues, please .