3 features released in Python 3.1 you should use in 2021

Explore some of the underutilized but still useful Python features.
53 readers like this.
Python programming language logo with question marks


This is the second in a series of articles about features that first appeared in a version of Python 3.x. Python 3.1 was first released in 2009, and even though it has been out for a long time, many of the features it introduced are underused and pretty cool. Here are three of them.

Thousands formatting

When formatting large numbers, it is common to place commas every three digits to make the number more readable (e.g., 1,048,576 is easier to read than 1048576). Since Python 3.1, this can be done directly when using string formatting functions:

"2 to the 20th power is {:,d}".format(2**20)
'2 to the 20th power is 1,048,576'

The ,d format specifier indicates that the number must be formatted with commas.

Counter class

The collections.Counter class, part of the standard library module collections, is a secret super-weapon in Python. It is often first encountered in simple solutions to interview questions in Python, but its value is not limited to that.

For example, find the five most common letters in the first eight lines of Humpty Dumpty's song:

hd_song = """
In winter, when the fields are white,
I sing this song for your delight.

In Spring, when woods are getting green,
I'll try and tell you what I mean.

In Summer, when the days are long,
Perhaps you'll understand the song.

In Autumn, when the leaves are brown,
Take pen and ink, and write it down.
import collections

collections.Counter(hd_song.lower().replace(' ', '')).most_common(5)
[('e', 29), ('n', 27), ('i', 18), ('t', 18), ('r', 15)]

Executing packages

Python allows the -m flag to execute modules from the command line. Even some standard-library modules do something useful when they're executed; for example, python -m cgi is a CGI script that debugs the web server's CGI configuration.

However, until Python 3.1, it was impossible to execute packages like this. Starting with Python 3.1, python -m package will execute the __main__ module in the package. This is a good place to put debug scripts or commands that are executed mostly with tools and do not need to be short.

Python 3.0 was released over 11 years ago, but some of the features that first showed up in this release are cool—and underused. Add them to your toolkit if you haven't already.

What to read next
Moshe sitting down, head slightly to the side. His t-shirt has Guardians of the Galaxy silhoutes against a background of sound visualization bars.
Moshe has been involved in the Linux community since 1998, helping in Linux "installation parties". He has been programming Python since 1999, and has contributed to the core Python interpreter. Moshe has been a DevOps/SRE since before those terms existed, caring deeply about software reliability, build reproducibility and other such things.

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.