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.
a = 'hello world!'
b = a.capitalize()
print('Original String:', a)
print('New String:', b)
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.
a = 'mumBai Is AWesome'
b = a.capitalize()
print('Original String:', a)
print('New String:', b)
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.
address='#1 Harbor Side'.capitalize()
print(address)
#1 harbor side