0

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

python123
  • 1
  • 1
  • 1
    How does output from `for i in range(0, 19): print(i)` contain space? – Austin Oct 24 '19 at 16:00
  • Possible duplicate of [How to print without newline or space?](https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space) – aloisdg Oct 24 '19 at 16:00
  • I don't think there are any spaces here, just line breaks. File #1 will contain line breaks after every number, essentially ending the file with an empty line. File #2 on the other hand ends at '18' – iliis Apr 20 '22 at 15:21

3 Answers3

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='')
blhsing
  • 91,368
  • 6
  • 71
  • 106
1

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.

David Camp
  • 322
  • 2
  • 9
0

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=",")
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103