'python - remove space in the end of range loop (int)
Its quite simple but I got stuck. I have two files who need to be identical(even spaces)
file #1 is the output from :
for i in range(0, 19):
print(i)
and the other is the same just without space after the 18
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
how do I remove the space after the 18 in order to get identical files?? ( only by changing file#1)
thank you! the example
Solution 1:[1]
You can call print
with newline as the separator and an empty string as the end marker instead:
print(*range(19), sep='\n', end='')
Solution 2:[2]
Try building the whole string first and then printing it:
m = " \n".join(map(str, range(19)))
print(m)
I recommend you use join
because you can specify a character that will only be inserted between the letters (in this case the separator is " \n"
) and not after the last one.
Solution 3:[3]
The answer can be “0,1,2,3,4,5,6,7,8,9.....18”, it should also include “try this end = " ," ”.
print(i, end=",")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | blhsing |
Solution 2 | David Camp |
Solution 3 | Suraj Rao |