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()
Below is the syntax for the Python
capitalize() method accepts no parameters.
Capitalize() Return Value
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)
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
" which is assigned to
The quick brown fox jumps over the lazy dog"
Also read: isalpha Python String Method with Advanced Examples
Capitalize() Doesn’t Change The Original String Example Program
capitalize() method returns a new string while leaving the original string unchanged.
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)
Original String: i shall RETURN. Applying capitalize(): I shall return.
In this instance, the
capitalize() method does not alter the original
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 converts the initial letter of each word to uppercase, producing the desired output.
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)
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
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
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.
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())
Title() method: Python For Free Capitalize() method: Python for free
Below is the comparison of two methods:
capitalize() method and
|Capitalize Method||Title Method|
|The ||With |
|Syntax: ||Syntax: |
|It does not take any parameters.||It does not take any parameters.|
|String is the return type.||String is the return type.|
Use the Python string
method to return a copy of a string with the first character converted to titlecase and the other characters converted to lowercase.