Python Capitalization Method with Examples

With the help of examples, this tutorial will teach you about the Python capitalization method.

What is Python capitalization?

The Python capitalization method returns a copy of the original string and changes the first character in the string to an upper case letter. All the other characters in the string become lowercase letters.

Additional information about capitalize built-in types from Python documentation

Return a copy of the string with its first character capitalized and the rest lowercased.

str.capitalize()

Capitalize() Syntax

Below is the syntax for the Python capitalize() method:

string.capitalize()

Capitalize() Parameter

The capitalize() method accepts no parameters.

Capitalize() Return Value

The capitalize() function returns a string in which the initial letter is capitalized.

Python Capitalize() Example Program

sample_string = "the Quick Brown Fox jumps Over The Lazy Dog"

# This will capitalize the first Letter of the string
new_string = sample_string.capitalize()
print(new_string)

Output:

The quick brown fox jumps over the lazy dog

In the above example, we used the capitalize() method to make the first letter of the sentence string uppercase and the rest lowercase. 

The result of sample_string.capitalize() is "The quick brown fox jumps over the lazy dog" which is assigned to new_string.

Also read: isalpha Python String Method with Advanced Examples


Capitalize() Doesn’t Change The Original String Example Program

The capitalize() method returns a new string while leaving the original string unchanged.

Example:

sample_string = "i shall RETURN."

# This will capitalize the first Letter of the string
new_string = sample_string.capitalize()

# This will print the original string
print("Original String:",sample_string)

# This will print the capitalize() string
print("Applying capitalize():",new_string)

Output:

Original String: i shall RETURN.
Applying capitalize(): I shall return.

In this instance, the capitalize() method does not alter the original sample_string.

Capitalize The First Letter of Each Word

To capitalize the first letter of each word in a string, the split() method is used to separate the given string into individual words. The generator expression iterates through the words and capitalizes the first letter of each word using the capitalize() method.

The capitalize() method converts the initial letter of each word to uppercase, producing the desired output.

Example:

sample_string = "the quick brown fox jumps over the lazy dog."
print("Original String:",sample_string)

# This will capitalize the first Letter of the string
new_string = ' '.join(every_word.capitalize() for every_word in sample_string.split())
print("New String:",new_string)

Output:

Original String: the quick brown fox jumps over the lazy dog.
New String: The Quick Brown Fox Jumps Over The Lazy Dog.

In this example, we use the split() method to split each word inside the string. After applying the split method, we used the capitalize() method to capitalize each word that had been split.

After that, we used the join() method to join all capitalized split strings and print them inside the new_string variable.

The title() method is better because it gives back a copy with every word’s first letter capitalized. 

Difference between title() and capitalize() in Python

The difference between title() and capitalize() in Python is that title() method returns a copy of the string where the first letters of all the words are capitalized, while the capitalize() method returns a copy of the string where only the first word is capitalized.

Example:

sample_string = "python for free"

# using the title method
print("Title() method:",sample_string.title())

# using the capitalize method
print("Capitalize() method:",sample_string.capitalize())

Output:

Title() method: Python For Free
Capitalize() method: Python for free

Below is the comparison of two methods: capitalize() method and title() method.

Capitalize MethodTitle Method
The capitalize() method changes strings in the Series/Index so that the first letter of the string is capitalized.With title() method, strings in the Series/Index are changed to titlecase.
Syntax: str.capitalize()Syntax: str.title()
It does not take any parameters.It does not take any parameters.
String is the return type. String is the return type. 
Difference between title() and capitalize() in Python

Summary

Use the Python string capitalize() method to return a copy of a string with the first character converted to titlecase and the other characters converted to lowercase.

Leave a Comment