The string module in the standard library provide various tools for working with strings. It contain several constants, functions, and classes to aid in string manipulation.

The builtin str class already provides a lot of methods for formatting strings. Thus the string module only contains additional tools that are not provided by the basic str class.

We can use the module after importing it in our Python program. 

#Use the string module

import string

S = "All animals are equal but some animals are more equal than others."

#capitalize all words in string S
print(string.capwords(S))

Constants defined in the string module

The module contains useful constants for working with unicode characters. The constants include:

string.ascii_letters

The asciii_letters attribute in the module returns a string containing all letters, both uppercase and lowercase(a-z, A-Z).

#import the string module
import string

#Use the ascii_letters constant
print(string.ascii_letters)
string.ascii_lowercase

Returns a string containing all lowercase letters(a-z).

import string

print(string.ascii_lowercase)
string.ascii_uppercase

Returns a string containing all uppercase letter(A-Z).

import string
print(string.ascii_uppercase)
string.digits

Returns a string containing all decimal digits (0-9)

import string
print(string.digits)
string.octdigits

Literally same as string.digits, above, i.e Returns a string containing decimal digits (0-9)

string.hexdigits 

Returns a string containing all hexadecimal digits (0-9, A-F, a-f)

import string 
print(string.hexdigits)
string.punctuation

Returns a string containing all punctuation characters.   

import string

print(string.punctuation)
string.printable 

A string containing all printable characters.  Printable characters are any characters that can be printed by a printer or displayed on a screen. This includes the alphabet, numbers, punctuation, and other symbols.

import string

print(string.printable)

'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

string.whitespace

A string containing all whitespace characters such as space, end of line character,  tab, carriage return, etc.

import string

print(r''.join(string.whitespace)) #We turn the values into a raw string for them to be visible.

' \t\n\r\x0b\x0c'

Functions defined in the module

function Usage
capwords(s) Used to capitalize all the words in the given string, s.

Classes defined in the module

Click on a class below to see its full syntax and usage.

class Usage
Template A class for supporting string substitutions.
Formatter A class for formatting strings in various ways such as padding and truncating.