Python Capitalize String using capitalize() and title()

In python capitalize() method capitalizes the first character of the string while the title() method capitalize the first character of the 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 first character of the each word in Uppercase and 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  change string to all uppercase letters and the  str.lower() method change string to all lowercase letters.