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?
Answers:
Try this:
print("hello",a , sep='')
sep
is the separator of print function when you call in with more that one argument.
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))
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
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?
Try this:
print("hello",a , sep='')
sep
is the separator of print function when you call in with more that one argument.
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))
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