Logo

Programming-Idioms

  • Go
  • PHP
  • JS
  • C

Idiom #302 String interpolation

Given the integer x = 8, assign to the string s the value "Our sun has 8 planets", where the number 8 was evaluated from x.

#include <stdio.h>
char s[32] = "";
sprintf(s, "Our sun has %i planets", x);
s := fmt.Sprintf("Our sun has %d planets", x)

Go doesn't have string interpolation, but it does have functions of the printf family.
$s = "Our sun has {$x} planets";
let s = `Our sun has ${x} planets`;

This is a "template literal". It's delimited with backticks.
s = std::format("Our sun has {} planets", x);

New implementation...
< >
programming-idioms.org