Snake_Byte #33: Fancier string formatting

Coming from a Javascript-heavy background, I didn’t spend much time reading up on Python’s string module. When I needed to concatenate some values into a string, I just jammed them together with + like so:

After spending more time with PokitDok projects and a little bit of time reading documentation, I came across the str.format method.

While some JS runtimes have more string formatting capabilities, Python’s format method is available out of the box and is a great way to clean things up.

So rather than saying something like this

We can do

The format method can also take keyword arguments if you need to concatenate more than a few values

Depending on how deep into this you want to go, there are plenty of other options. This website has an extensive collection of examples.

Leave a Reply

Your email address will not be published.