Println and tuple format query


Hi all,

I’m trying to print out a tuple I have:

t1 = (9,6,5,2)
println("Tuple1, position1: $t1[1]")

but I receive an error because of the 1 inside the bracket, whats the correct way to format the statement?

Just to add, I have made the statement work just fine using

@printf("Tuple1, Position 1: %d", t1[1])

But I would also like to learn how to use it with println.



You need parentheses to do the interpolation:

t1 = (9,6,5,2)
println("Tuple1, position1: $(t1[1])")

The interpolation only takes the very first thing it sees and no more — so $foo(x) just interpolates foo and not the function call. Same with indexing.


You can also use multiple arguments with println:

println("Tuple1, position1: “, t1[1])

many thanks to you both!

Happy coding