String Interpolation

In computer programming, string interpolation is the process of replacing placeholders with values in a string literal. A string is a sequence of characters. A string literal is a sequence of characters from the source character set enclosed in quotation marks. They are used to represent a sequence of characters that form a null-terminated string. String interpolation is a form of simple template processing and allows for easier and more intuitive string formatting and content-specification.

For example, if a template is made to say hello to a person, such as “hello {first name}”, the goal would be to replace the placeholder for the first name with a name of an actual person. String interpolation works to complete this task.

Placeholders are typically represented by a bare or a sigil, such as $ or %. String interpolation is common in programming languages that use string representations of data, such as Python, Scala, and Ruby. String interpolation is beneficial for code clarity, reducing memory use, and increasing speed.

String interpolation examples

Examples of string interpolation within different programming languages can be found below.

Python

Program to run in Python:

name = 'World'
program = 'Python'
print(f'Hello {name}! This is {program}')

Output of the program:

Hello World! This is Python

JavaScript

Program to run in JavaScript:

var apples = 4;
var bananas = 3;
console.log(`I have ${apples} apples`);
console.log(`I have ${apples + bananas} fruit`);

Output of the program:

I have 4 apples                                                         
I have 7 fruit

C#

Program to run in C#:

var apples = 4;
var bananas = 3;
// Before C# 6.0
Console.WriteLine(String.Format("I have {0} apples", apples));
Console.WriteLine(String.Format("I have {0} fruit", apples + bananas));
// C# 6.0
Console.WriteLine($"I have {apples} apples");
Console.WriteLine($"I have {apples + bananas} fruit");

Output of the program:

I have 4 apples                                                     
I have 7 fruit

PHP

Program to run in PHP:

<?php
$apples = 5;
$bananas = 3;
echo "There are $apples apples and $bananas bananas.";
echo "n";
echo "I have ${apples} apples and ${bananas} bananas.";

Output of the program:

There are 5 apples and 3 bananas.                                          
I have 5 apples and 3 bananas.

Webopedia Staff
Since 1995, more than 100 tech experts and researchers have kept Webopedia’s definitions, articles, and study guides up to date. For more information on current editorial staff, please visit our About page.

Top Articles

The Complete List of 1500+ Common Text Abbreviations & Acronyms

Text Abbreviations reviewed by Web Webster   From A3 to ZZZ we list 1,559 SMS, online chat, and text abbreviations to help you translate and understand...

Windows Operating System History & Versions

The Windows operating system (Windows OS) refers to a family of operating systems developed by Microsoft Corporation. We look at the history of Windows...

How to Create a Website Shortcut on Your Desktop

Website Shortcut on Your Desktop reviewed by Web Webster   This Webopedia guide will show you how to create a website shortcut on your desktop using...

Generations of Computers (1st to 5th)

Reviewed by Web Webster Learn about each of the 5 generations of computers and major technology developments that have led to the computing devices that...

Telecommunication

Telecommunication refers to telephony and cellular network technology. However, the broader definition includes...

Spoofing

What is spoofing? As it pertains to cybersecurity, spoofing is when a person disguises...

How to Indent in...

Microsoft Word is a graphical word...