10

let's say that I have string:

    s = "Tuple: "

and Tuple (stored in a variable named tup):

    (2, a, 5)

I'm trying to get my string to contain the value "Tuple: (2, a, 5)". I noticed that you can't just concatenate them. Does anyone know the most straightforward way to do this? Thanks.

Jacob Griffin
  • 4,219
  • 3
  • 15
  • 11

3 Answers3

32

This also works:

>>> s = "Tuple: " + str(tup)
>>> s
"Tuple: (2, 'a', 5)"
Bi Rico
  • 24,433
  • 3
  • 49
  • 72
11

Try joining the tuple. We need to use map(str, tup) as some of your values are integers, and join only accepts strings.

s += "(" + ', '.join(map(str,tup)) + ")"
Jack
  • 720
  • 4
  • 20
7
>>> tup = (2, "a", 5)
>>> s = "Tuple: {}".format(tup)
>>> s
"Tuple: (2, 'a', 5)"
Fred
  • 999
  • 1
  • 7
  • 34