Searched a lot about this but it is not working in Python 3.7.3 Shell.
There a lot of similar question (for example Remove and Replace Printed items which refers to an 8 years old Python version though), where they succesfully solve the problem using control characters \r
and \b
. But in my case I'm not able to solve the problem.
I would like to have an output like amount: x/y
(where x current position and y the length of a string or a list) which is updated at each iteration, ie the part x/y
has to be deleted and the part x+1/y
has to be written, while keeping the string amount:
.
What I tried so far
(1)
import sys
import time
word='simple'
for i in range(len(word)):
if i+1 < 10:
print('amount: '+'0'+str(i+1)+'/'+str(len(word)+1), end='\r')
else:
print('amount: '+str(i+1)+'/'+str(len(word)+1), end='\r')
time.sleep(0.5)
output
(2)
import sys
import time
word='simple'
for i in range(len(word)):
if i+1 < 10:
sys.stdout.write('amount: '+'0'+str(i+1)+'/'+str(len(word)))
else:
sys.stdout.write('amount: '+str(i+1)+'/'+str(len(word)))
sys.stdout.write("\b \b")
time.sleep(0.5)
output