Julia Language Strings String interpolation (insert value defined by variable into string)


Example

In Julia, as in many other languages, it is possible to interpolate by inserting values defined by variables into strings. For a simple example:

n = 2
julia> MyString = "there are $n ducks"
"there are 2 ducks"

We can use other types than numeric, e.g.

Result = false
julia> println("test results is $Result")
test results is false

You can have multiple interpolations within a given string:

MySubStr = "a32"
MyNum = 123.31
println("$MySubStr  ,   $MyNum")

Performance Tip Interpolation is quite convenient. But, if you are going to be doing it many times very rapidly, it is not the most efficient. Instead, see Convert numeric types to strings for suggestions when performance is an issue.