1

With an f-string you can fill in 'placeholders' in a string directly with some variable value. Opposed to, let's say using the str.format() method, often seen.

Example:

In [1]: x=3
In [2]: y=4
In [3]: print(f'The product of {x} and {y} is {x*y}.')
The product of 3 and 4 is 12.

Is this concept of f-strings only found in python? Or is there any other language that supports this, maybe under a different name?

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
muuh
  • 1,013
  • 12
  • 24

3 Answers3

3

Short answer: No - not unique to python. In python it was introduced with 3.6 by Literal String Interpolation - PEP 498

In C# there is $ - string interpolation as shorthand for some function you would use String.Format for:

string val = "similar";
string s = $"This is {val}"     // s == "This is similar"

// vs. 

var s2 = string.Format ("This is {0}", val); // {0} = 0th param that follows
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • 1
    Note that C# interpolated strings are not always a shortcut for string.Format, if they can they just use string.Concat – blenderfreaky Aug 23 '20 at 20:08
1

You can find it under the name of Template Literals in Javascript as well. In particular, see the section Expression Interpolation for an example similar as the one in OP's question.

Alessandro Cosentino
  • 2,268
  • 1
  • 21
  • 30
1

The shell: Starting in the Dark Ages shells used to expand parameters in double quoted strings (and unquoted arguments too).

Perl: Perl has a shellish side, and had interpolation in double quoted strings from at least the time I bought the 1st edition of the Camel book and started learning the language.

I think that other people may be more specific on the precise date of introduction of string interpolation in Perl, but I suspect it was there from the beginning.

Many other programming languages, especially those using sigils, share this concept of interpolation.

psmears
  • 26,070
  • 4
  • 40
  • 48
gboffi
  • 22,939
  • 8
  • 54
  • 85