Python String Interpolation

Spread the love

Python String Interpolation is not complicated. It is replacing placeholders with some value or values in a specific order.

Click to start video above

Place holder Python code below

// Working Example of string interpolation
print("this is how it is {x} and the age is {z}".format(x="Peter",z=28))

Python String Interpolation: Wikipedia definition

In computer programmingstring interpolation (or variable interpolationvariable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values. It is a form of simple template processing[1] or, in formal terms, a form of quasi-quotation (or logic substitution interpretation). String interpolation allows easier and more intuitive string formatting and content-specification compared with string concatenation.[2]

More Python String Interpolation examples

# in all versions
oranges = 4
print("I have %d oranges" % oranges) # no longer recommended
print("I have %(oranges)d oranges" % locals())  # no longer recommended
# with Python 2.6+
print("I have {0} oranges".format(oranges))
print("I have {a} oranges".format(a=oranges))
# with Python 2.7+
print("I have {} oranges".format(oranges))
# or with Python 3.6+
print(f"I have {oranges} oranges")

This is the python string output

I have 4 Oranges

Python string interpolation

Go back to the top of the Python String Interpolation