Home > other >  How do I count from 1 to 10 with the line removed when it ends? ( python )
How do I count from 1 to 10 with the line removed when it ends? ( python )

Time:10-06

How do I count from 1 to 10 with the line removed when it ends?? on python

CodePudding user response:

@Hassan's code works, and the explanation is because when you print out a string to the console, it doesn't flush itself unless you print a newline "\n", and because the print() function appends by default "\n" to every string, it flushes itself every time by default. To avoid that you need to add a keyword argument end="" to print() so it won't flush and you can print again to the same line using the \r character at the beginning, which lets you override what is written in that line.

CodePudding user response:

try that:

for i in range(11):
print(f'\r{i}', end='')

To see the result slowly:

import time

for i in range(11):
    print(f'\r{i}', end='')
    time.sleep(1)
  • Related