Tutorial Study Image

Python capitalize()

The capitalize() function in python helps to convert the first character of the string to uppercase and all the remaining characters to lowercase if any. The converted string is returned as output without affecting the original string.


string.capitalize() 
 

capitalize() Parameters:

The capitalize() function doesn't take any parameter. If the first character of the string is already a capital letter it will return the original string.

capitalize() Return Value

The return value of this method is a copy of the original string by changing its first character into uppercase all remaining as lowercase. If the string is empty, it will return an empty string as output. Again if we use a numeric string like "12", it will return an error.

Input Return Value
If string string with the first character in uppercase

Examples of capitalize() method in Python

Example 1: how to capitalize a sentence in Python?


string = "hiii How are You."

capitalized = string.capitalize()

print('Old String is: ', string)
print('Capitalized String is:', capitalized)
 

Output:


Old String is: hiii How are You.
Capitalized String is: Hiii how are you.

Example 2: Working with Non-alphabetic first character


string = "* is a multiple operator."

updated_string = string.capitalize()

print('Old String is:', string)
print('New String is:', updated_string)
 

Output:


Old String is: * is a multiple operator.
New String is: * is a multiple operator.