pythonpython-3.xlanguage-conceptsf-string

Are f-strings a unique feature to python (3.6 and higher)?


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?


Solution

  • 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