0

Is there any way to avoid printing out data (like quotations marks, brackets, commas and other important matematical symbols) when printing from a variable?

int1 = random.randint(1,100)
int2 = random.randint(1,100)
q1 = "What is", int1, "+", int2, "?"
answer = int(raw_input(q1))

The code above prints this:

('What is', 75, '+', 74, '? ')149

The proper way of printing the above would supposedly be this:

What is 75 + 74? 149
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
Shoryu
  • 225
  • 2
  • 3
  • 9
  • 2
    `q1` is a tuple. If you wanted to make a string use `+` to concatenate and explicitly convert numbers to string using `str`. – Bakuriu Apr 10 '14 at 11:23

3 Answers3

3

The "proper way" is to use str.format:

q1 = "What is {0} + {1}? ".format(int1, int2)
answer = int(raw_input(q1))
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
2

The clearest syntax I believe would be the one that separates the constant string from the variables:

print "What is %d + %d?" % (int1, int2)
bosnjak
  • 8,424
  • 2
  • 21
  • 47
0

Simple:

q1 = "What is" +  str(int1) +  "+" + str(int2) + "?"

or

' '.join([str(x) for x in q1])
Sufian Latif
  • 13,086
  • 3
  • 33
  • 70