Interpolation in Julia

Find this useful? Support us: Star on GitHub 6
Category: String | Language: Julia

Interpolation is the process of inserting a variable's value within a string. To perform string interpolation in Julia, the variable's name is enclosed in curly braces, {} inside the string. Here's an example:

name = "Alice"
age = 25
println("My name is $name and I'm $age years old.")

Output:

My name is Alice and I'm 25 years old.

In the above example, the variables name and age are interpolated into the string using the $ symbol inside the string.

Alternatively, you can also use the $(expression) syntax to interpolate complex expressions into the string. Here's an example:

radius = 10
println("The area of a circle with radius $radius is $(pi*radius^2).")

Output:

The area of a circle with radius 10 is 314.1592653589793.

Here, the expression pi*radius^2 is evaluated and its result is interpolated into the string.