Python String capitalize() Method
The capitalize()
method returns the copy of a string with its first character capitalized and the rest lowercased.
Syntax
string.capitalize()
Parameters
No parameters
Return Value
Returns a new string with the first character capitalized and the rest lowercased.
Examples
Basic Usage
text = "hello world"
print(text.capitalize()) # Output: Hello world
The capitalize()
method only capitalizes the first character of the entire string.
If you are trying to capitalize each word, use the title()
method.
With the already capitalized string
If the string is already capitalized, the first character stays uppercase, while all other characters are converted to lowercase.
text = "Hello World"
print(text.capitalize()) # Output: Hello world
With non-alphabetic first character
If the first character of the string is not an alphabet (e.g., number, symbol, or space), it remains unchanged. However, any subsequent alphabetic characters will still be converted to lowercase.
text = "123 Hello World"
print(text.capitalize()) # Output: 123 hello world