Python String Module

Filed Under: Python String

Python String module contains some constants, utility function, and classes for string manipulation.

Python String Module

It’s a built-in module and we have to import it before using any of its constants and classes.

String Module Constants

Let’s look at the constants defined in the string module.

import string

# string module constants
print(string.whitespace)  # ' \t\n\r\x0b\x0c'




python string module constants

string capwords() function

Python string module contains a single utility function – capwords(s, sep=None). This function split the specified string into words using str.split(). Then it capitalizes each word using str.capitalize() function. Finally, it joins the capitalized words using str.join().

If the optional argument sep is not provided or None, then leading and trailing whitespaces are removed and words are separated with single whitespace. If it’s provided then the separator is used to split and join the words.

s = '  Welcome TO  \n\n JournalDev '

Output: Welcome To Journaldev

python string capwords

Python String Module Classes

Python string module contains two classes – Formatter and Template.


It behaves exactly same as str.format() function. This class become useful if you want to subclass it and define your own format string syntax.

Let’s look at a simple example of using Formatter class.

from string import Formatter

formatter = Formatter()
print(formatter.format('{website}', website='JournalDev'))
print(formatter.format('{} {website}', 'Welcome to', website='JournalDev'))

# format() behaves in similar manner
print('{} {website}'.format('Welcome to', website='JournalDev'))


Welcome to JournalDev
Welcome to JournalDev

python string module Formatter class


This class is used to create a string template for simpler string substitutions as described in PEP 292. It’s useful in implementing internationalization (i18n) in an application where we don’t need complex formatting rules.

from string import Template

t = Template('$name is the $title of $company')
s = t.substitute(name='Pankaj', title='Founder', company='JournalDev.')

Output: Pankaj is the Founder of JournalDev.

Python string module Template class

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation


  1. Rohit says:

    Wow thanks.

  2. Mayank says:

    I didn’t get the thing searched by me

    1. Gagan says:

      Same here Mayank, I’m disappointed with this article

      1. Pankaj says:

        Just saying that you didn’t found what you were looking for doesn’t make any sense. Please specify what is missing so that I can add it up or point you to another post. As of now, your comment has no value.

        1. Syed says:

          Ur good man don’t pay attention to negativity. Good job

Leave a Reply

Your email address will not be published. Required fields are marked *

Generic selectors
Exact matches only
Search in title
Search in content