DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Data
  4. How to Implement String Interpolation in Python

How to Implement String Interpolation in Python

String interpolation is a process of substituting values of local variables into placeholders in a string. Let's see how it's done in Python.

Ivan Mushketyk user avatar by
Ivan Mushketyk
·
Jan. 20, 17 · Tutorial
Like (6)
Save
Tweet
Share
27.89K Views

Join the DZone community and get the full member experience.

Join For Free

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


Perl:

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


CoffeeScript:

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


... and many others.

At 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.

But let’s start with basics. An idiomatic way to build a complex string in Python is to use the “format” function:

print "Hi, I am {} and I am {} years old".format(name, age)
>>> Hi, I am John and I am 26 years old


Which is much cleaner than using string concatenation:

print "Hi, I am " + name + " and I am " + str(age) + " years old"
Hi, I am John and I am 26 years old


But if you use the format function in this way, the output depends on the order of arguments:

print "Hi, I am {} and I am {} years old".format(age, name)
Hi, I am 26 and I am John years old


To avoid that we can pass key-value arguments to the “format” function:

print "Hi, I am {name} and I am {age} years old".format(name="John", age=26)
Hi, I am John and I am 26 years old

print "Hi, I am {name} and I am {age} years old".format(age=26, name="John")
Hi, I am John and I am 26 years old


Here we had to pass all variables for string interpolation to the “format” function, but still we have not achieved what we wanted, because “name” and “age” are not local variables. Can “format” somehow access local variables?

To do it we can get a dictionary with all local variables using the locals function:

name = "John"
age = 26

locals()
>>> {
 ...
 'age': 26,
 'name': 'John',
 ...
}


Now we just need to somehow pass it to the format function. Unfortunately we cannot just call it as s.format(locals()):

print "Hi, I am {name} and I am {age} years old".format(locals())
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-5-0fb983071eb8> in <module>()
----> 1 print "Hi, I am {name} and I am {age} years old".format(locals())

KeyError: 'name'


This is because locals returns a dictionary, while format expects key-value parameters.

Luckily, we can convert a dictionary into key-value parameters using \*\* opeartor. If we have a function that expects key-value arguments:

def foo(arg1=None, arg2=None):
    print "arg1 = " + str(arg1)
    print "arg2 = " + str(arg2)


We can pass parameters packed in a dictionary:

d = {
    'arg1': 1,
    'arg2': 42
}

foo(**d)
>>> arg1 = 1
arg2 = 42


Now we can use this technique to implement our first version of string interpolation:

print "Hi, I am {name} and I am {age} years old".format(**locals())
Hi, I am John and I am 26 years old


It works but looks cumbersome. With this approach every time we need to interpolate our string we would need to write format(\*\*locals()). 

It would be great if we could write a function that would interpolate a string like this:

# Can we implement inter() function in Python?
print inter("Hi, I am {name} and I am {age} years old")
>>> Hi, I am John and I am 26 years old


At first it seems impossible, since if we move interpolation code to another function it would not be able to access local variables from a scope where it was called from:

name = "John"
print inter("My name is {name}")

...

def inter(s):
  # How can we access "name" variable from here?
  return s.format(...)


And yet, it is possible. Python provides a way to inspect the current stack with the sys.\_getframe function:

import sys

def foo():
     foo_var = 'foo'
     bar()


 def bar():
     # sys._getframe(0) would return frame for function "bar"
     # so we need to to access 1-st frame
     # to get local variables from "foo" function
     previous_frame = sys._getframe(1)
     previous_frame_locals = previous_frame.f_locals
     print previous_frame_locals['foo_var']


foo()
>>> foo


So the only thing that is left is to combine frames introspection with the format function. Here are 2 lines of code that would do the trick:

def inter(s):
    return s.format(**sys._getframe(1).f_locals)

name = "John"
age = 26

print inter("Hi, I am {name} and I am {age} years old")
>>> Hi, I am John and I am 26 years old
Strings Data Types Interpolation Python (language)

Published at DZone with permission of Ivan Mushketyk, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 3 Main Pillars in ReactJS
  • How To Build an Effective CI/CD Pipeline
  • Microservices Testing
  • A Beginner's Guide to Infrastructure as Code

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: