I would like to do something like this:
function say(name, age)
println("$name is $age!")
end
But this gives me an error because Julia thinks age! is the name of the variable. If I add a space between $age and ! then the printed string has a space between age and !, which I don't want. I tried \! which I saw elsewhere but my current Julia version gives me invalid escape sequence error.
Just add brackets
println("$name is $(age)!")
The accepted answer is great, but just in case you want other ways to do it, here are two more ways (though not using string interpolation as in your question):
function say1(name, age)
println(name, " is ", age, "!")
end
function say2(name, age)
println(string(name, " is ", age, "!"))
end
say1("Tom", 32)
## Tom is 32!
say2("Tom", 32)
## Tom is 32!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With