Python Capitalize String using capitalize() and title()
In Python, capitalize()
method capitalizes the first character of the string while the title()
method capitalizes the first character of each word of the string.
Python capitalize() method
string = "this is a python string"
print(string.capitalize())
The above Python code will change the first character of the variable string to Uppercase.
This is a python string
Python title() Method
Python title()
method will return the string with the first character of each word in Uppercase and the rest of the characters in lowercase.
Example
string = "this is a python string"
print(string.title())
As per the above python code, each word in the variable string will be capitalized.
This Is A Python String
Two other useful string functions are upper()
and lower()
. The str.upper()
method changes the string to all uppercase letters and the str.lower()
method changes the string to all lowercase letters.