Skip to content

Tag: interpolation

How to implement string interpolation in Python

String interpolation is a process of substituting values of local variables into placeholders in a string.

It is implemented in many programming languages such as Scala:

//Scala 2.10+
var name = "John";
println(s"My name is $name")
>>> My name is John


my $name = "John";
print "My name is $name";
>>> My name is John


name = "John"
console.log "My name is #{name}"
>>> My name is John

and many others.

On the first sight, it doesn’t seem that it’s possible to use string interpolation in Python. However, we can implement it with just 2 lines of Python code.

Leave a Comment