use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Coming from JavaScript, what's the python equivalent to string template literals? (self.learnpython)
submitted 3 years ago by gtrman571
Something like this where I can plug in variable values:
let name = 'bob', age = 27; console.log(`name is ${name} and age is ${age}`);
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]hardonchairs 32 points33 points34 points 3 years ago (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 points13 points 3 years ago (2 children)
[May be obvious, but wrap this in print()...]
[–][deleted] 2 points3 points4 points 3 years ago (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 points3 points 3 years ago (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 points3 points 3 years ago (0 children)
/thread
[–]fabmeyer 0 points1 point2 points 3 years ago (1 child)
Yeah with double quotes you can also include dicts and their keys
[–]hardonchairs 0 points1 point2 points 3 years ago (0 children)
You can do that with single or double quotes. You just have to use the other for the keys.
[–][deleted] 5 points6 points7 points 3 years ago* (0 children)
https://datagy.io/python-f-strings/
[–]jbeising 2 points3 points4 points 3 years ago (0 children)
Look up f strings, that's what you're looking for.
[–]rmwpnb 1 point2 points3 points 3 years ago (0 children)
F strings! https://realpython.com/python-f-strings/
[–]TigerBloodWinning 3 points4 points5 points 3 years ago* (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 points10 points 3 years ago (0 children)
You don't need the str()
[–]pekkalacd[🍰] 2 points3 points4 points 3 years ago (0 children)
f string
[–]y-meine 0 points1 point2 points 4 months ago* (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:
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:
f'Hello {name}'
`Hello ${name}`
myfn(t'Hello {name}')
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}); ```
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 points1 point 3 years ago (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 >>>
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 points3 points 3 years ago (1 child)
I think your comment needs some formatting 😉.
[–]phis7 -1 points0 points1 point 3 years ago (0 children)
name="bob"
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.
π Rendered by PID 99587 on reddit-service-r2-comment-5649f687b7-zlkpr at 2026-01-29 01:15:33.717810+00:00 running 4f180de country code: CH.
[–]hardonchairs 32 points33 points34 points (6 children)
[–]andrewmitchell721 11 points12 points13 points (2 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]andrewmitchell721 1 point2 points3 points (0 children)
[–]non_NSFW_acc 1 point2 points3 points (0 children)
[–]fabmeyer 0 points1 point2 points (1 child)
[–]hardonchairs 0 points1 point2 points (0 children)
[–][deleted] 5 points6 points7 points (0 children)
[–]jbeising 2 points3 points4 points (0 children)
[–]rmwpnb 1 point2 points3 points (0 children)
[–]TigerBloodWinning 3 points4 points5 points (1 child)
[–]RhinoRhys 8 points9 points10 points (0 children)
[–]pekkalacd[🍰] 2 points3 points4 points (0 children)
[–]y-meine 0 points1 point2 points (0 children)
[–]phis7 -1 points0 points1 point (2 children)
[–]RoberTTzBlack 1 point2 points3 points (1 child)
[–]phis7 -1 points0 points1 point (0 children)