Python Strings

String capitalization

Python offers several methods to manipulate the capitalization of characters within a string. You can convert all characters to uppercase using upper(), convert all to lowercase with lower(), capitalize the first letter of each word with title(), or capitalize only the first letter of the entire string with capitalize(). These functions are essential for various text processing tasks, such as standardizing user input or formatting output.

Let’s dive into each of these methods with explanations and examples.


1. upper(): Convert to uppercase

The upper() method returns a new string where all characters are converted to uppercase.

Syntax:

string.upper()

Example:

text = "hello world"
uppercase_text = text.upper()

print(uppercase_text)  # Output: "HELLO WORLD"

In this example, all lowercase letters are converted to uppercase.


2. lower(): Convert to lowercase

The lower() method returns a new string where all characters are converted to lowercase.

Syntax:

string.lower()

Example:

text = "HELLO WORLD"
lowercase_text = text.lower()

print(lowercase_text)  # Output: "hello world"

Here, all uppercase letters are converted to lowercase.


3. capitalize(): Capitalize the first letter

The capitalize() method returns a new string where the first character is capitalized and all other characters are converted to lowercase.

Syntax:

string.capitalize()

Example:

text = "python programming"
capitalized_text = text.capitalize()
print(capitalized_text)  # Output: "Python programming"

In this example, only the first letter of the string is capitalized, while the rest is in lowercase.


4. title(): Capitalize each word

The title() method returns a new string where the first character of each word is capitalized, and all other characters in each word are lowercase. This is useful for formatting titles or headings.

Syntax:

string.title()

Example

text = "python programming is fun"
title_text = text.title()

print(title_text)  # Output: "Python Programming Is Fun"

In this case, the first letter of each word is capitalized, while the remaining letters in each word are lowercase.


5. swapcase(): Swap Case of Each Character

The swapcase() method returns a new string where the case of each character is swapped. Uppercase letters are converted to lowercase, and lowercase letters are converted to uppercase.

Syntax:

string.swapcase()

Example

text = "Hello World"
swapped_text = text.swapcase()

print(swapped_text)  # Output: "hELLO wORLD"

In this example, all uppercase letters are converted to lowercase and vice versa.


Additional notes:

  • Remember that strings in Python are immutable, meaning the original string is not modified. All these methods return a new string with the case modifications.
  • These methods only affect alphabetic characters (`a-z` and `A-Z`). Non-alphabetic characters, such as numbers or punctuation, remain unchanged.

It's time to take a quiz!

Test your knowledge and see what you've just learned.

Which Python method converts all characters in a string to uppercase?

capitalize()
upper()
lower()
Check Answer

Which Python method converts all characters in a string to lowercase?

upper()
lower()
title()
Check Answer

Which Python method capitalizes only the first letter of the entire string?

upper()
lower()
capitalize()
Check Answer

Which Python method capitalizes the first letter of each word in a string?

capitalize()
lower()
title()
Check Answer

Which Python method swaps the case of each character in a string (uppercase to lowercase and vice versa)?

title()
capitalize()
swapcase()
Check Answer