Replace print statements in Python 2.7

How to replace print statements in Python 2.7, for example:

for i in range(100):
    print i,"% Completed"

#Result Must Be:
>>> 1 % Completed 

1 should be replaced by 2 and so on. But not on another line or add on the same line.

I tried to find it, found a solution, for example

from __future__ import print_function
print("i: "+str(i),end="\r")

But they lead to the attached print statements. Is there any correction in mine print_functionor is there another solution?

+4
source share
2 answers

You want something like this:

>>> import sys, time
>>> for i in range(100):
...     time.sleep(0.1)
...     sys.stdout.write('%d %% Completed \r' % (i,))
...     sys.stdout.flush()
+4
source

Here you go:

from __future__ import print_function

for i in range(100):
    print('{0}% Completed\r'.format(i), end='')
print('100% Completed')

Set a higher range if you really want it to work for a few seconds.

+2
source

Source: https://habr.com/ru/post/1620595/


All Articles