Python print syntactic sugar

Since Python 3.6 introduced f-strings I’ve been trying to shake the habit of using .format I developed when re-learning python 2. As I work in a number of different languages I find the embedded {foo} syntax to be more familiar and less special case in nature, much nicer than the older % without parens with one argument and with parens with two, and in general more flexible. So I was pleasantly surprised when one of the shinier new f-string features came up in conversation.

In python 3.8, which I installed to experiment with using pyenv, you can use a nice piece of syntactic sugar to shorten your debugging print statements. Instead of writing the long form of print with the variable name repeated like this:

    print(f"User is first_name={first_name}")
    > User is first_name=Dean

    # or the older python 2 supported

    print("User is first_name={}".format(first_name))
    > User is first_name=Dean

You can now use the shorter and less repetitional:

    print(f"User is {first_name=}")
    > User is first_name='Dean'

You can even embed some white space in the output to make it a little prettier.

    print(f"User is {first_name = } {last_name = }")
    User is first_name = 'Dean' last_name = 'Wilson'

This isn’t a massive change to the language, and it’s not generally available yet (3.8 isn’t a published release as I write this) but it is a nice little language improvement for the little chunks of python I occasionally write.