Question

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
Was it helpful?

Solution 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)

OTHER TIPS

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

q1 = "What is {0} + {1}? ".format(int1, int2)
answer = int(raw_input(q1))

Simple:

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

or

' '.join([str(x) for x in q1])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top