ExampleEdit & Run
#Use the capwords function

#import the string module
import string

#call the capwords function
print(string.capwords("no pain, no gain."))
Output:
No Pain, No Gain.[Finished in 0.01729909493587911s]

The string module in the Python's standard library provides a number of tools for manipulating strings. Among them is the capwords() function which capitalizes each word in the input string. 

Syntax:
capwords(s, sep = None)
s A required argument representing the string to capitalize.
sep An optional argument used to indicate a delimiter for the input string.  It defaults to a whitespace(" ")
Parameters:

The function returns a new string with all the words of the input string capitalized.

In order to use the function, we must first import the string module in our program.

ExampleEdit & Run
import string

print(string.capwords("all animals are equal, but some animals are more equal than others."))
Output:
All Animals Are Equal, But Some Animals Are More Equal Than Others.[Finished in 0.017414458096027374s]

Example with sep given

ExampleEdit & Run
import string

print(string.capwords("two_legs_bad_four_legs_good", sep = "_"))
Output:
Two_Legs_Bad_Four_Legs_Good[Finished in 0.017412916058674455s]

How the function works

The function splits the input string into words using the split() method, capitalizes each word using the capitalize() method, and finally joins the capitalized words using the join() method.   

If the optional second argument sep is absent or None, adjoining whitespace characters are replaced by a single space while leading and trailing whitespace are removed, otherwise sep is used to split and join the words.