How to Capitalize the First Letter of each Word in a String in Python

Created
Modified

Using title Method

The str.title() method returns a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase.

The following example should cover whatever you are trying to do:

#!/usr/bin/python3

s = "method of a string"
print(s.title())
Method Of A String

Using capwords Method

Split the argument into words using str.split(), capitalize each word using str.capitalize(), and join the capitalized words using str.join(). For example,

#!/usr/bin/python3

# Import module
import string

s = "method of a string"
print(string.capwords(s))
Method Of A String

Related Tags