ArticleslgStudy

computer science

String interpolation

String interpolation is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand String interpolation rather than just read about it. In short: In computer programming, string interpolation (or variable interpolation, variable 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 or, in formal terms, a form of quasi-quotation (or logic substitution interpretatio…

Key takeaways

  • String interpolation belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect String interpolation to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of String interpolation from memory before moving on to harder problems.

Reference excerpt

In computer programming, string interpolation (or variable interpolation, variable 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 or, in formal terms, a form of quasi-quotation (or logic substitution interpretation). The placeholder may be a variable name, or in some languages an arbitrary expression, in either case evaluated in the current context. String interpolation is an alternative to building a string via concatenation, which requires repeat quoting and unquoting; or substituting into a printf format string, where the variable is far from where it is used. Consider this example in Ruby:

Two types of literal expression are usually offered: one with interpolation enabled, the other without. Non-interpolated strings may also escape sequences, in which case they are termed a raw string, though in other cases this is separate, yielding three classes of raw string, non-interpolated (but escaped) string, interpolated (and escaped) string. For example, in Unix shells, single-quoted strings are raw, while double-quoted strings are interpolated. Placeholders are usually represented by a bare or a named sigil (typically $ or %), e.g. $apples or %apples, or with braces, e.g. {apples}, sometimes both, e.g. ${apples}. In some cases, additional formatting specifiers can be used (as in printf), e.g. {apples:3}, and in some cases the formatting specifiers themselves can be interpolated, e.g. {apples:width}. Expansion of the string usually occurs at run time. Language support for string interpolation varies widely. Some languages do not offer string interpolation, instead using concatenation, simple formatting functions, or template libraries. String interpolation is common in many programming languages which make heavy use of string representations of data, such as Apache Groovy, Julia, Kotlin, Perl, PHP, Python, Ruby, Scala, Swift, Tcl and most Unix shells.

Algorithms There are two main types of variable-expanding algorithms for variable interpolation:

Replace and expand placeholders: creating a new string from the original one, by find–replace operations. Find variable reference (placeholder), replace it with its variable value. This algorithm offers no cache strategy. Split and join string: splitting the string into an array, merging it with the corresponding array of values, then joining items by concatenation. The split string can be cached for reuse.

Security issues

String interpolation, like string concatenation, may lead to security problems. If user input data is improperly escaped or filtered, the system will be exposed to SQL injection, script injection, XML external entity (XXE) injection, and cross-site scripting (XSS) attacks. An SQL injection example:

query = "SELECT x, y, z FROM Table WHERE id='$id' "

If $id is replaced with "'; DELETE FROM Table; SELECT * FROM Table WHERE id='", executing this query will wipe out all the data in Table.

Examples

ABAP

The output will be:

Bash

The output will be:

Boo

The output will be:

C

C does not have interpolated strings, but they can be approximated using sprintf() and related functions from <stdio.h>.

C++

While interpolated strings do not exist in C++, some proposals for the feature exist; currently, they can be approximated using std::format and std::print functions.

C#

The output will be:

ColdFusion Markup Language

ColdFusion Markup Language (CFML) script syntax:

Tag syntax:

The output will be:

I have 4 apples

CoffeeScript

The output will be:

Dart

The output will be:

Go

While there have been some proposals for string interpolation (which have been rejected), As of 2025 Go does not have interpolated strings. However, they can be approximated using fmt.Sprintf().

Groovy

In groovy, interpolated strings are known as GStrings:

The output will be:

Haxe

The output will be:

Java

Java had interpolated strings as a preview feature in Java 21 and Java 22 (see java.lang.StringTemplate). They were removed in Java 23 due to design issues. They can otherwise be approximated using java.lang.String's format() static method or formatted() instance method, or java.text.MessageFormat.

JavaScript/TypeScript

JavaScript and TypeScript, as of the ECMAScript 2015 (ES6) standard, support string interpolation using backticks ``. This feature is called template literals. Here is an example:

The output will be:

Julia

The output will be:

Kotlin

The output will be:

Nemerle

It also supports advanced formatting features, such as:

The output will be:

Nim

Nim provides formatted string literals via the strformat module. The fmt template verifies that the format string is well-formed and well-typed, and then expands it into Nim source code at compile-time.

The output will be:

Nix

The output will be:

ParaSail

The output will be:

Perl

The output will be:

PHP

The output will be:

Python

Python supports string interpolation as of version 3.6, referred to as "formatted string literals" or "f-strings". Such a literal begins with an f or F before the opening quote, and uses braces for placeholders:

The output will be:

Ruby/Crystal

The output will be:

Rust

Rust does not have general string interpolation, but provides similar functionality via macros, referred to as "Captured identifiers in format strings", introduced in version 1.58.0, released 2022-01-13. Rust provides formatting via the std::fmt module, which is interfaced with through various macros such as format!(), write!(), and print!(). These macros are converted into Rust source code at compile-time, whereby each argument interacts with a std::fmt::Formatter. The formatter supports positional parameters, named parameters, argument types, defining various formatting traits, and capturing identifiers from the environment.

The output will be:

Scala

Scala provides a general facility to allow arbitrary processing of a string literal, and supports string interpolation using the included s and f string interpolators (introduced in Scala 2.10). It is also possible to write custom ones or override the standard ones. The f interpolator is a compiler macro that rewrites a format string with embedded expressions as an invocation of String.format. It verifies that the format string is well-formed and well-typed. Scala's string interpolation allows embedding variable references directly in processed string literals. Here is an example:

The output will be:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with String interpolation

Start with the simplest possible case. Write down what String interpolation claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to String interpolation before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about String interpolation ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of String interpolation

In research
String interpolation appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses String interpolation in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
String interpolation is common in secondary-school and first-year university syllabi. It links to neighbouring topics Programming constructs, String (computer science), Variable (computer science), so understanding it makes those chapters shorter.
In everyday life
Look for String interpolation outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “String interpolation” →

How to study String interpolation in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what String interpolation means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain String interpolation out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is String interpolation in simple terms?

In computer programming, string interpolation (or variable interpolation, variable 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…

Why does String interpolation matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study String interpolation?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on String interpolation.

Tags

  • Programming constructs
  • String (computer science)
  • Variable (computer science)

Keep exploring