67: JavaScript templates strings in Python


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 thought on “67: JavaScript templates strings in Python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s