-4

I have following code:

for i in range(1,6):
    print 'Answer',i,':'

Output is:

Answer 1 :
Answer 2 :
Answer 3 :
Answer 4 :

I want it to be like this:

Answer 1:
Answer 2:
Answer 3:
Answer 4:

i.e. without spaces in between integer and ':' How to do this?

Ravi Ojha
  • 722
  • 1
  • 6
  • 15

4 Answers4

6

Use string formatting:

for i in range(1, 5):
    print 'Answer {0}:'.format(i)
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
1

Try using this:

for i in range(1,5):
    print "Answer %d:" % i
arulmr
  • 8,620
  • 9
  • 54
  • 69
1

Alternative:

for i in range(1,6):
    print 'Answer '+str(i)+':'
Thanakron Tandavas
  • 5,615
  • 5
  • 28
  • 41
0
for i in range(1, 5):
    print "Answer", str(i)+':'

When you are printing with ',', the space is added automatically, you can concatenate output by either using + or positioning strings next to each other (in Python 2.x), like so:

for i in range(1, 5):
    print 'Answer'+str(i), ':'
    print 'Answer%d'':'%i

Check the difference!

catalesia
  • 3,348
  • 2
  • 13
  • 9