Thursday, 20 July 2017

python - Print in one line dynamically



I would like to make several statements that give standard output without seeing newlines in between statements.



Specifically, suppose I have:




for item in range(1,100):
print item


The result is:



1
2
3
4

.
.
.


How get this to instead look like:



1 2 3 4 5 ...



Even better, is it possible to print the single number over the last number, so only one number is on the screen at a time?


Answer



Change print item to:




  • print item, in Python 2.7

  • print(item, end=" ") in Python 3



If you want to print the data dynamically use following syntax:





  • print(item, sep=' ', end='', flush=True) in Python 3


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...