Edmonds Karp
Edmonds Karp

Reputation: 165

Julia string interpolation followed by exclamation mark

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.

Upvotes: 4

Views: 232

Answers (2)

Julia Learner
Julia Learner

Reputation: 2922

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!

Upvotes: 4

Michael K. Borregaard
Michael K. Borregaard

Reputation: 8044

Just add brackets

println("$name is $(age)!")

Upvotes: 10

Related Questions