thiagowfx's avatar

¬ just serendipity 🍀 (not just serendipity)

Python: debugging tip with print and f-strings

• 140 words • 1 min • updated

⚠️ This post is over one year old. It may no longer be up to date or relevant. Opinions may have changed.

It’s 2024, the year of the linux desktop, and the best™ way to debug computer programs is still the good ol’ print statement.

Since Python 3.6 it is possible to use f-strings.

One of my favorite ways to use them for debugging is with the equal sign (=):

To display both the expression text and its value after evaluation, (useful in debugging), an equal sign '=' may be added after the expression.

Here is one example:

python
def is_full_word_match(token, words):
    print(f'  is_full_word_match: {token=} {words=}')
    return token in words

If you call it like so:

python
is_full_word_match("hello", "hello world")

Then it will print the following:

  is_full_word_match: token='hello' words='hello world'

This is a more ergonomic (and quicker) way to write than the classic:

python
print('  is_full_word_match: token=' + token + ' words=' + words)

Or even:

python
print('  is_full_word_match: token={} words={}'.format(token, words))