I would like to make several statements that give standard output without seeing newlines in between statements.
Specifically, suppose I have:
for it
For those struggling as I did, I came up with the following that appears to work in both python 3.7.4 and 3.5.2.
I expanded the range from 100 to 1,000,000 because it runs very fast and you may not see the output. This is because one side effect of setting end='\r'
is that the final loop iteration clears all of the output. A longer number was needed to demonstrate that it works.
This result may not be desirable in all cases, but was fine in mine, and OP didn't specify one way or another. You could potentially circumvent this with an if statement that evaluates the length of the array being iterated over, etc.
The key to get it working in my case was to couple the brackets "{}"
with .format()
. Otherwise, it didn't work.
Below should work as-is:
#!/usr/bin/env python3
for item in range(1,1000000):
print("{}".format(item), end='\r', flush=True)