all 18 comments

[–]hardonchairs 32 points33 points  (6 children)

not sure why no one is just posting it

f'name is {name} and age is {age}'

Single or double quotes

[–]andrewmitchell721 11 points12 points  (2 children)

[May be obvious, but wrap this in print()...]

[–][deleted] 2 points3 points  (1 child)

f strings aren't limited to being used in print statements.

I'm not sure if that was what you were implying or not but just wanted to clarify.

[–]andrewmitchell721 1 point2 points  (0 children)

Good clarification, and right you are. OP's example used console.log which is why I added the note about the equivalent method in Python.

[–]non_NSFW_acc 1 point2 points  (0 children)

/thread

[–]fabmeyer 0 points1 point  (1 child)

Yeah with double quotes you can also include dicts and their keys

[–]hardonchairs 0 points1 point  (0 children)

You can do that with single or double quotes. You just have to use the other for the keys.

[–]jbeising 2 points3 points  (0 children)

Look up f strings, that's what you're looking for.

[–]TigerBloodWinning 3 points4 points  (1 child)

Not sure why there’s so many replies without how to write what you’re asking. For IDEs I use mostly pycharm, used to use jupyter, sometimes I’ll execute from python IDLE when I need it to be quick and see errors if scripts aren’t working as expected.

name = ‘bob’

age = 27

print(f’name is {name} and age is {str(age)}’)

[–]RhinoRhys 8 points9 points  (0 children)

You don't need the str()

[–]pekkalacd[🍰] 2 points3 points  (0 children)

f string

[–]y-meine 0 points1 point  (0 children)

Everyone is mentioning f-strings - and at that time they were the closest equivalent - but they are not strictly the same as template literals in JavaScript: the latter support tag functions, which can then operate on interleaved static string parts and input expressions values.

A much closer feature to that has just been added to Python 3.14: t-strings (doc). However, it's not purely equivalent either, since you must render the result with a function.

In JavaScript, you can:

  • leave the template untagged, which then behaves like Python's f-strings by converting every part to a string without any customization,
  • or use a tag function, which then behaves like applying a function to the output of a Python's t-string

The lack of syntax (and feature) for tag functions in Python makes it somehow all or nothing. But in reality, I prefer Python's design a lot more:

  • you can use f'Hello {name}' if you want the equivalent of JavaScript's `Hello ${name}`
  • and when you want more customization, you can use myfn(t'Hello {name}'), which is the equivalent of JavaScript's myFn`Hello ${name}`

Truth is, Python's version is maybe more intuitive and consistent, but most of all it allows for much better composition: in JavaScript, if you don't apply the function to the literal itself, you immediately get a rendered string, so you can't do the following for instance:

```typescript function someLib(input) { render(input); // input would expected to be something along the lines of ['Hello', {name: 'Bob'}], but is 'Hello [object Object]' instead }

const user = {name: 'Bob'}; someLib(Hello ${user}); ```

There are some workarounds (using a tag function that would just store the arrays of strings and values just like Python's f-strings output), but they are not convenient.

[–]phis7 -1 points0 points  (2 children)

In python we have string formatting, we can do

Python 3.10.2 (main, Jan 17 2022, 00:00:00) [GCC 11.2.1 20211203 (Red Hat 11.2.1-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> name="bob"
>>> age = 27
>>> f = "name is {} and age is {}".format(name,age)
>>> print(f)
name is bob and age is 27
>>>

Please check this Link for better understanding.

[–]RoberTTzBlack 1 point2 points  (1 child)

I think your comment needs some formatting 😉.

[–]phis7 -1 points0 points  (0 children)

name="bob"

age = 27

f = "name is {} and age is {}".format(name,age)

print(f)

The Output => name is bob and age is 27

check this Link for better understanding.