Python capitalize() Method

The capitalize() method returns the copy of the string with its first character capitalized and the rest of the letters lowercased.

Syntax:

string.capitalize()

Parameters:

No parameters.

Return Value:

Returns a capitalized string.

The following example converts the first letter of a word to capital.

Example: Capitalize a word
a = 'hello world!' 
b = a.capitalize()        
print('Original String:', a)  
print('New String:', b)   
Output
Original String: hello world!
New String: Hello world!

Notice that the method does not change the original string on which it is being called. It returns a new string.

The following example capitalizes a sentence.

Example: Capitalizing a Sentence
a = 'mumBai Is AWesome'
b = a.capitalize()
print('Original String:', a)  
print('New String:', b)
Output
Original String: mumBai Is AWesome
New String: Mumbai is awesome

In the above example, only the first letter of the sentence is capitalized because the method only capitalizes the first letter of the string. The rest of the letters are lowercased even if they are capital in the original string.

If the first letter is non-alphabetic, then the method doesn’t convert it to a capital letter, but it converts all other letters to lowercase.

Example: Ignores Symbols & Numbers
address='#1 Harbor Side'.capitalize()
print(address)
Output
#1 harbor side
Want to check how much you know Python?