0
Is there any function to print a line in the middle of console dynamically in python?
I want to create a box in console using symbols and print strings inside that box. strings are given by user.
1 Answer
+ 2
Use print item, to make the print statement omit the newline.
In Python 3, it's print(item, end=" ").
If you want every number to display in the same place, use for example (Python 2.7):
to = 20
digits = len(str(to - 1))
delete = "\b" * (digits + 1)
for i in range(to):
print "{0}{1:{2}}".format(delete, i, digits),
In Python 3, it's a bit more complicated; here you need to flush sys.stdout or it won't print anything until after the loop has finished:
import sys
to = 20
digits = len(str(to - 1))
delete = "\b" * (digits)
for i in range(to):
print("{0}{1:{2}}".format(delete, i, digits), end="")
sys.stdout.flush()