Python String capitalize()

In Python, the capitalize() method converts first character of a string to uppercase letter and lowercases all other characters, if any.

The syntax of capitalize() is:

string.capitalize()

capitalize() Parameter

The capitalize() function doesn't take any parameter.


Return Value from capitalize()

The capitalize() function returns a string with first letter capitalized and all other characters lowercased. It doesn't modify the original string.


Example 1: Capitalize a Sentence

string = "python is AWesome."

capitalized_string = string.capitalize()

print('Old String: ', string)
print('Capitalized String:', capitalized_string)

When you run the program, the output will be:

Old String: python is AWesome
Capitalized String: Python is awesome

Example 2: Non-alphabetic First Character

string = "+ is an operator."

new_string = string.capitalize()

print('Old String:', string)
print('New String:', new_string)

When you run the program, the output will be:

Old String: + is an operator.
New String: + is an operator.