To provide a basic example, say I wanted to write:
name = str(input())
age = int(input())
print('Hi, {name}, you are {age}.')
In javascript, this would look like:
console.log(`Hi, ${name}, you are ${age}.`)
I assume there is no direct implementation of template literals in Python, as I haven't found any mention on Google / DDG.
If I am correct in thinking that there isn't a direct implementation, have any of you found workarounds? Thanks in advance.
Template is a class of String module. It allows for data to change without having to edit the application. It can be modified with subclasses. Templates provide simpler string substitutions . Templates substitution is “$“-based substitutions.
Template literals are literals delimited with backtick ( ` ) characters, allowing for multi-line strings, string interpolation with embedded expressions, and special constructs called tagged templates.
Template Literals is an ES6 feature (JavaScript 2015).
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With