Removing Extra spaces in Python using a variable in 'for' Loop

Question:

In a code:

a = 6
for i in range(a):
    print("case #",i)

While printing the output, as shown below:

case # 0
case # 1
case # 2
case # 3
case # 4
case # 5

There is an extra space between ‘#’ and the variable ‘i’.
How to remove this space?
In fact, if we normally write:

a = 'sam'
print("hello",a)

then also, the output is:

hello sam

there is an extra space between “hello” and “sam”. How to remove this?

Asked By: VIKAS RATHEE

||

Answers:

Try this:

print("hello",a , sep='')

sep is the separator of print function when you call in with more that one argument.

Answered By: Mehrdad Pedramfar

You can just make one string from the two arguments, using +. Note: you have to convert i to string first.

a = 6
for i in range(a):
    print("case #"+str(i))
Answered By: Piotrek

Could you please try following too once.

a=6
for i in range(a):
   print 'case #%d' % (i)

Output will be as follows.

case #0
case #1
case #2
case #3
case #4
case #5
Answered By: RavinderSingh13
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.