Matthew Goddard

Playing the infinite game


67: JavaScript templates strings in Python

23/01/2023

When I first started learning Python, the one thing I wanted more than anything was to be able to use template strings, like had gotten used to in JavaScript.

language = "JavaScript"
string = `this is a string literal in ${language}`

For some reason, I couldn’t find a decent article telling me how to do them.

Then on Sunday, when I asked ChatGTP to generate a piece of code for me, I was struck by something in the output.

url = f"https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/token"

So I asked ChatGTP what the ‘f’ meant, and it told me about ‘f-strings’ for ‘formatted string literals` in python, which is exactly what I was I was looking for.

So here you are. If you want to use template strings in python, then prefix the string with an f, and you’re done!



One response to “67: JavaScript templates strings in Python”

  1. […] been posting the odd snippet about this  (68: Generative AI you need to check its working outs, 67: JavaScript templates strings in Python, 66: Python: MS Graph API Authentication (Without a user)).When I get the chance, I’ll post […]

Leave a Reply

Discover more from Matthew Goddard

Subscribe now to keep reading and get access to the full archive.

Continue reading