String interpolation

2 snippets across 2 stacks - JavaScript, Python

JSJavaScript

Template Literals

JS · Strings
Syntax
`text ${expression} text`
Example
const item = "coffee";
const price = 4.5;
console.log(`One ${item} costs $${price.toFixed(2)}.`);

const multiline = `Line one
Line two
Line three`;
console.log(multiline);
Output
"One coffee costs $4.50."
"Line one\nLine two\nLine three"

Note Backtick strings support embedded expressions and multiline content without escape characters.

PYPython

f-strings (Formatted String Literals)

PY · Strings
Syntax
f"text {expression}"
Example
name = "Alice"
balance = 1234.5
print(f"User: {name}")
print(f"Balance: ${balance:,.2f}")
print(f"{'centered':^20}")
Output
User: Alice
Balance: $1,234.50
      centered      

Note Since Python 3.12, f-strings can contain backslashes and nested quotes freely. You can also nest f-strings inside f-strings.

Frequently asked questions

How does JavaScript handle string interpolation?
This task is covered in 2 stacks on this page: JavaScript, Python. The "Template Literals" snippet in JavaScript uses ``text ${expression} text``.
Which code does the JavaScript example use?
The "Template Literals" snippet uses ``text ${expression} text``, from the Strings section of the JavaScript cheat sheet.
Which stacks cover "string interpolation" on this page?
JavaScript, Python. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Template Literals": Backtick strings support embedded expressions and multiline content without escape characters.