Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia string interpolation followed by exclamation mark

Tags:

julia

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.

like image 303
Edmonds Karp Avatar asked Dec 07 '22 12:12

Edmonds Karp


2 Answers

Just add brackets

println("$name is $(age)!")
like image 115
Michael K. Borregaard Avatar answered Mar 13 '23 18:03

Michael K. Borregaard


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!
like image 44
Julia Learner Avatar answered Mar 13 '23 18:03

Julia Learner