String interpolation is the replacement of defined character sequences in a string by given values. This representation may be considered more intuitive for formatting and defining content than the composition of multiple strings and values using concatenation operators. String interpolation is typically implemented as a language feature in many programming languages including PHP, Haxe, Perl, Ruby, Python, C# (as of 6.0) and others.
Questions tagged [string-interpolation]
1093 questions
129
votes
4 answers
Multiline C# interpolated string literal
C# 6 brings compiler support for interpolated string literals with syntax:
var person = new { Name = "Bob" };
string s = $"Hello, {person.Name}.";
This is great for short strings, but if you want to produce a longer string must it be specified on…

Drew Noakes
- 300,895
- 165
- 679
- 742
115
votes
16 answers
How do I interpolate strings?
I want to do the following in C# (coming from a Python background):
strVar = "stack"
mystr = "This is %soverflow" % (strVar)
How do I replace the token inside the string with the value outside of it?

sazr
- 24,984
- 66
- 194
- 362
114
votes
3 answers
C# 6 how to format double using interpolated string?
I have used interpolated strings for messages containing string variables like $"{EmployeeName}, {Department}". Now I want to use an interpolated string for showing a formatted double.
Example
var aNumberAsString =…

MagB
- 2,131
- 5
- 28
- 54
113
votes
8 answers
How to solve "String interpolation produces a debug description for an optional value; did you mean to make this explicit?" in Xcode 8.3 beta?
Since beta 8.3, zillions warnings "String interpolation produces a debug description for an optional value; did you mean to make this explicit?" appeared in my code.
For example, the warning popped in the following situation up, where options could…

Stéphane de Luca
- 12,745
- 9
- 57
- 95
111
votes
7 answers
How can I use escape characters with string interpolation in C# 6?
I've been using string interpolation and love it. However, I have an issue where I am trying to include a backslash in my output, but I am not able to get it to work.
I want something like this...
var domain = "mydomain";
var userName =…

Matt
- 1,109
- 2
- 7
- 4
102
votes
3 answers
PowerShell string interpolation syntax
I always used the following syntax to be sure that variable were expanded in a string:
"my string with a $($variable)"
I recently ran into the following syntax:
"my string with a ${variable}"
Are they equivalent? Any difference?

fra
- 3,488
- 5
- 38
- 61
100
votes
6 answers
Use YAML with variables
Are variables within YAML files possible? For example:
theme:
name: default
css_path: compiled/themes/$theme.name
layout_path: themes/$theme.name
In this example, how can theme: name: default be used in other settings? What is the syntax?

brewster
- 4,342
- 6
- 45
- 67
95
votes
1 answer
Why does string interpolation work in Ruby when there are no curly braces?
The proper way to use string interpolation in Ruby is as follows:
name = "Ned Stark"
puts "Hello there, #{name}" #=> "Hello there, Ned Stark"
That is the way I intend to always use it.
However, I've noticed something odd in Ruby's string…

Charles Caldwell
- 16,649
- 4
- 40
- 47
92
votes
5 answers
f-strings vs str.format()
I'm using the .format() a lot in my Python 3.5 projects, but I'm afraid that it will be deprecated during the next Python versions because of f-strings, the new kind of string literal.
>>> name = "Test"
>>> f"My app name is {name}."
'My app name is…

nivhanin
- 1,688
- 3
- 19
- 31
84
votes
6 answers
Is there a formatted byte string literal in Python 3.6+?
I'm looking for a formatted byte string literal. Specifically, something equivalent to
name = "Hello"
bytes(f"Some format string {name}")
Possibly something like fb"Some format string {name}".
Does such a thing exist?

Enrico Borba
- 1,877
- 2
- 14
- 26
84
votes
5 answers
String concatenation vs. interpolation in Ruby
I am just starting to learn Ruby (first time programming), and have a basic syntactical question with regards to variables, and various ways of writing code.
Chris Pine's "Learn to Program" taught me to write a basic program like…

Jeff H.
- 923
- 1
- 7
- 9
80
votes
3 answers
Does Rust support Ruby-like string interpolation?
In Ruby I could do this.
aaa = "AAA"
bbb = "BBB #{aaa}"
puts(bbb)
> "BBB AAA"
The point of this syntax is eliminating repetition, and making it to feel like a shell script - great for heavy string manipulation.
Does Rust support this? Or have…

eonil
- 83,476
- 81
- 317
- 516
79
votes
2 answers
C# interpolated string with conditional-operator
I tried to use the conditional operator inside an interpolated string, but because it has a colon in it, the compiler thinks that after the colon comes a format string.
$"test {foo ? "foo is true" : "foo is false"}";
How can I use this type of…

wertzui
- 5,148
- 3
- 31
- 51
79
votes
12 answers
Java generating Strings with placeholders
I'm looking for something to achieve the following:
String s = "hello {}!";
s = generate(s, new Object[]{ "world" });
assertEquals(s, "hello world!"); // should be true
I could write it myself, but It seems to me that I saw a library once which did…

ndrizza
- 3,285
- 6
- 27
- 41
74
votes
1 answer
PHP variable interpolation vs concatenation
What is the difference between the two following methods (performance, readability, etc.) and what do you prefer?
echo "Welcome {$name}s!"
vs.
echo "Welcome " . $name . "!";

Aley
- 8,540
- 7
- 43
- 56