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. 

ExampleEdit & Run
#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))
copy
Output:
All Animals Are Equal But Some Animals Are More Equal Than Others. [Finished in 0.017187550198286772s]

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).

ExampleEdit & Run
#import the string module
import string

#Use the ascii_letters constant
print(string.ascii_letters)
copy
Output:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ [Finished in 0.016575943678617477s]
string.ascii_lowercase

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

ExampleEdit & Run
import string

print(string.ascii_lowercase)
copy
Output:
abcdefghijklmnopqrstuvwxyz [Finished in 0.016624208074063063s]
string.ascii_uppercase

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

ExampleEdit & Run
import string
print(string.ascii_uppercase)
copy
Output:
ABCDEFGHIJKLMNOPQRSTUVWXYZ [Finished in 0.016833768226206303s]
string.digits

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

ExampleEdit & Run
import string
print(string.digits)
copy
Output:
0123456789 [Finished in 0.016396805178374052s]
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)

ExampleEdit & Run
import string 
print(string.hexdigits)
copy
Output:
0123456789abcdefABCDEF [Finished in 0.01674799807369709s]
string.punctuation

Returns a string containing all punctuation characters.   

ExampleEdit & Run
import string

print(string.punctuation)
copy
Output:
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ [Finished in 0.016086765099316835s]
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)
copy
Output:
'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.
copy
Output:
' \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.