Python: how to print without white spaces?
Question:
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?
Answers:
Use string formatting:
for i in range(1, 5):
print 'Answer {0}:'.format(i)
Try using this:
for i in range(1,5):
print "Answer %d:" % i
Alternative:
for i in range(1,6):
print 'Answer '+str(i)+':'
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!
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?
Use string formatting:
for i in range(1, 5):
print 'Answer {0}:'.format(i)
Try using this:
for i in range(1,5):
print "Answer %d:" % i
Alternative:
for i in range(1,6):
print 'Answer '+str(i)+':'
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!