Published on

Handling uppercase and lowercase transformations in Python

Authors
  • avatar
    Name
    hwahyeon
    Twitter

Here are Python's string methods for handling uppercase and lowercase transformations, along with explanations and examples using the sentence "Hello World":

capitalize():

Converts the first character of the string to uppercase and the rest to lowercase.

sentence = "hello world"
result = sentence.capitalize()
print(result)  # Output: "Hello world"

title():

Converts the first character of each word to uppercase and the rest to lowercase.

sentence = "hello world"
result = sentence.title()
print(result)  # Output: "Hello World"

upper():

Converts all characters in the string to uppercase.

sentence = "Hello World"
result = sentence.upper()
print(result)  # Output: "HELLO WORLD"

lower():

Converts all characters in the string to lowercase.

sentence = "Hello World"
result = sentence.lower()
print(result)  # Output: "hello world"

swapcase():

Swaps the case of all characters in the string (uppercase becomes lowercase, and vice versa).

sentence = "Hello World"
result = sentence.swapcase()
print(result)  # Output: "hELLO wORLD"