Python string module

A very useful but little known module in Python is the “string” module.

It rarely gets attention because Python provides a lot of the functionalities for strings on all string values, but this “string” module is almost always used on a specific use case which I’ll mention later in this blogpost.

We don’t even have to use an IDE or text editor to explore this module —- just go inside a Python interactive terminal by typing “python” on the console/command-prompt.

>python

Depending on what you’ve installed, you’ll get a response about the version and some other information.
On my environment, I’m using Python 3.7.4 and this is what appears onmy terminal:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Okay, by default, the “string” module is not loaded so we have to load it via the import statement:

import string

We can take a look at what this module has to offer by using the “dir” function:

>>> dir(string)

['Formatter', 'Template', '_ChainMap', '_TemplateMetaclass', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']

Let’s try some of them and see what they do:

>>> string.digits
'0123456789'
string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

Interestingly, there’s a property that seems to combine them all (and more):

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

You might say, “nothing spectacular about those” or “sheesh, I can just do it on a normal module using variables”, right?

And you’d be correct. The point of using this module or practically any module is to have a common or standard way of doing things or having the same set of data like this so our codes are portable and can be used by a lot of people and produce the same results. Or, if we copy codes from the net, it ensures that what they see is what you’ll see (since you’re using the same set of codes essentially).

And here’s one cool function that I’m sure you can find a use case in one of your projects — the “capwords” function.

If you want to know what it does, just pass that to the “help” function:

>>> help(string.capwords)

Help on function capwords in module string:

capwords(s, sep=None)
    capwords(s [,sep]) -> string

    Split the argument into words using split, capitalize each
    word using capitalize, and join the capitalized words using
    join.  If the optional second argument sep is absent or None,
    runs of whitespace characters are replaced by a single space
    and leading and trailing whitespace are removed, otherwise
    sep is used to split and join the words.

>>>

Here’s an example of what it does and how you might want to use it:

>>> stmt = "the quick brown fox jumps over the lazy dog"
>>>
>>> string.capwords(stmt)
'The Quick Brown Fox Jumps Over The Lazy Dog'

You could (for example) use this to convert lyrics of a song in text files, or maybe a food recipe, comment field in a database, etc.

As promised, I’ll tell you what this module is usually used for — and that is, when generating temporary passwords.

I’m sure you’ve encountered this on web sites where you sign up and you are given a “temporary password” that you’ll use on initial login and then you can go ahead and modify, right?

Well, on the next blog I’ll be writing a function that does that using this “string” module so stay tuned. 🙂

Leave a Comment