You can go with formatted string literals ("f-strings") since Python 3.6
f"Hi {name}, you are {age}"
Or string formatting
"Hi {}, you are {}".format(name, age)
"Hi {name}, you are {age}".format(name=name, age=age)
Or format specifiers
"Hi %s, you are %d" % (name, age)
Answer from Abhilash on Stack Overflow Top answer 1 of 2
85
You can go with formatted string literals ("f-strings") since Python 3.6
f"Hi {name}, you are {age}"
Or string formatting
"Hi {}, you are {}".format(name, age)
"Hi {name}, you are {age}".format(name=name, age=age)
Or format specifiers
"Hi %s, you are %d" % (name, age)
2 of 2
-3
You can try this.
print('Hello',name,'have a nice day!')
Python
peps.python.org โบ pep-0750
PEP 750 โ Template Strings - Python Enhancement Proposals
July 8, 2024 - Like Template, it is a new class found in the string.templatelib module: class Interpolation: value: object expression: str conversion: Literal["a", "r", "s"] | None format_spec: str __match_args__ = ("value", "expression", "conversion", "format_spec") def __new__( cls, value: object, expression: str = "", conversion: Literal["a", "r", "s"] | None = None, format_spec: str = "", ): ...
Coming from JavaScript, what's the python equivalent to string template literals?
not sure why no one is just posting it f'name is {name} and age is {age}' Single or double quotes More on reddit.com
Add tagged template literals - Ideas - Discussions on Python.org
Feature or enhancement Thanks for such a great language and ecosystems! And I love Pythonโs f-string. I zapped some website, Javascript has Tagged template literals, and it is a powerful feature. Template literals (Template strings) - JavaScript | MDN Tagged Template Literals - Wes Bos Pitch ... More on discuss.python.org
Printing template literals in python "{item}" vs. "{}.format(item)"
Amanda Esping is having issues with: Is there a reason why/helpful way to remember which format literal is used where? In most of the places I've learned so far, it works like this:... More on teamtreehouse.com
PEP 501: (reopen) General purpose string template literals - PEPs - Discussions on Python.org
With collaboration with Nick Coghlan (the original author of PEP 501), we would like to propose reopening PEP 501 after 7 years of being differed. In those seven years, f-strings have become common idiomatic python, and people are familiar and comfortable with them. More on discuss.python.org
Videos
04:37
Python 3.14's new template string feature - YouTube
16:41
Python 3.14 - Template Strings deep dive! - YouTube
16:35
Python 3.14: The NEW T-strings are Awesome - YouTube
19:29
Exploring Python T-Strings: Formatting Limitations & Introducing ...
Javascript Tutorial: Template Literals
00:56
Using Template Literals instead of Concatenation #shorts - YouTube
Reddit
reddit.com โบ r/learnpython โบ coming from javascript, what's the python equivalent to string template literals?
r/learnpython on Reddit: Coming from JavaScript, what's the python equivalent to string template literals?
March 26, 2022 -
Something like this where I can plug in variable values:
let name = 'bob', age = 27;
console.log(`name is ${name} and age is ${age}`);LaunchCode
education.launchcode.org โบ lchs โบ chapters โบ strings โบ template-literals.html
7.7. Template Literals โ LaunchCode's LCHS documentation
Also, concatenation usually requires several test runs of the code in order to check for syntax errors and proper spacing. Fortunately, Python offers us a better way to accomplish the same thing. Template literals allow for the automatic insertion of expressions and variable values into strings.
GeeksforGeeks
geeksforgeeks.org โบ python โบ template-class-in-python
String Template Class in Python - GeeksforGeeks
July 23, 2025 - Explanation: This code creates a template with placeholders $name and $city, then prints the original template string using the .template attribute without performing any substitutions. Since $ is used to mark placeholders, you must use $$ if you want a literal dollar sign $ in your output.
Python
peps.python.org โบ pep-0501
PEP 501 โ General purpose template literal strings | peps.python.org
August 8, 2015 - The template string is parsed into literals, expressions, format specifiers, and conversion specifiers as described for f-strings in PEP 498 and PEP 701. The syntax for conversion specifiers is relaxed such that arbitrary strings are accepted (excluding those containing {, } or :) rather than being restricted to valid Python identifiers.
Python
docs.python.org โบ 3 โบ library โบ string.html
string โ Common string operations
Advanced usage: you can derive subclasses of Template to customize the placeholder syntax, delimiter character, or the entire regular expression used to parse template strings. To do this, you can override these class attributes: delimiter โ This is the literal string describing a placeholder introducing delimiter.
Top answer 1 of 2
8
Hey @amandae (https://teamtreehouse.com/amandae), There are many ways to format a string (https://docs.python.org/3/tutorial/inputoutput.html#input-and-output) in Python.
The f-strings (https://docs.python.org/3/tutorial/inputoutput.html#formatted-string-literals), formatted string literals, is the latest addition, first appearing in Python 3.6 (https://docs.python.org/3/whatsnew/3.6.html#pep-498-formatted-string-literals).
The more complex (and flexible) previous method is using the str.format() (https://docs.python.org/3/tutorial/inputoutput.html#the-string-format-method) method. This is is still widely used, and has additional features (like repeatable, reorderable fields) so expect it to stick around for a long time.
With the format method, the {} placeholders can taking on many forms:
* as order-dependent placeholders when empty: "{} before {}".format(first, last)
* as indexed-dependent placeholders when numbered: "{1} before {0}".format(goes2zero, goes2one)
* as named placeholders: "{first} before {last}".format(first=goes2first, last=goes2last)
So, about remembering the difference. The "f" in front of f-strings can be thought of as fancy, where it is a shortcut to the named placeholder version of format without having to add the .format(first=first, last=last) method. The "f" can also be thought of as fast, since f-stings have been optimized to be the fasted string handling in python!!
Post back if you need more help. Good luck!!
2 of 2
0
Thank you so much! Your answer is wonderfully helpful!
Stack Abuse
stackabuse.com โบ formatting-strings-with-the-python-template-class
Formatting Strings with the Python Template Class
September 19, 2021 - We'll then have a look at how we can change the way our Templates can substitute data into strings. For a better understanding of these topics, you'll require some basic knowledge on how to work with classes and regular expressions. The Python Template class was added to the string module since Python 2.4.
Programiz
programiz.com โบ python-programming โบ string-interpolation
Python String Interpolation
For instance, if you have a template for saying hello to a person like "Hello {Name of person}, nice to meet you!", you would like to replace the placeholder for name of person with an actual name. This process is called string interpolation. Python 3.6 added new string interpolation method called literal ...
Educative
educative.io โบ answers โบ string-templates-in-python
String templates in Python
String templates in Python simplify generating dynamic outputs while separating program logic from output formats. Using the string.Template class with methods like substitute() and safe_substitute(), developers can create customizable web pages, reports, and XML files efficiently.
Python.org
discuss.python.org โบ peps
PEP 501: (reopen) General purpose string template literals - PEPs - Discussions on Python.org
March 9, 2023 - With collaboration with Nick Coghlan (the original author of PEP 501), we would like to propose reopening PEP 501 after 7 years of being differed. In those seven years, f-strings have become common idiomatic python, and โฆ