Python 3.6 String Interpolation

Python 3.6 introduces a new Ruby-style string interpolation

If you're one of those Ruby/Python purists look away now.

I first learned about this new Python 3 syntax for string interpolation around six months ago, and since then I've barely used it because I hadn't used it enough to remember the syntax — isn't that frustrating? And since it's recent, Googling for it wasn't returning much either — I hadn't actually realised this feature was unique to versions 3.6 onward, so I wasn't looking in the correct documentation versions.

Essentially Python 3.6 introduces a new form of string interpolation that is similar to the way one can embed the values of variables into strings in Ruby. I've always enjoyed using that feature in Ruby so it's great that Python 3 has adopted it.

This is the feature in Ruby I'm talking about:

msg = "Hello at last!"
puts "#{msg}"

And this is the same example using the new syntax in Python 3.6:

msg = "Hello at last!"
print(f"{msg}")

Output:

>> Hello at last!

Which I find to be a lot nicer than my previously-preferred method in Python:

"{msg}".format(msg=msg)

In most situations at least.