1

Is it possible to create scrolling text in the Python command line by repeatedly updating the same line of text with small time.sleep() delays?

I believe that the \b (backspace) character can effectively move the cursor backward over text already written. I thought that combining this with end="" parameter in Python 3 might allow subsequent print commands to update a previous print command. With careful tracking of the length of the text and insertion of backspace and other characters it should be possible to create static lines of text that animate in place.

Unfortunately none of this works and even the \b character seems to do nothing:

word = input("Type something-> ")
print(word+"\b\b\bHello", end="")
print("New text")

Anyone got any ideas?

Many thanks, Kw

3
  • 2
    Take a look at the ncurses module if you are working on a unixish OS Commented May 20, 2018 at 11:33
  • Another approach would to simple clear any text in the command line between updates of the scrolling text, but again I'm not sure this is possible. Commented May 20, 2018 at 11:35
  • You may be able to use VT100 Terminal Control Escape Sequences, eg stackoverflow.com/a/37501797/4014959 Commented May 20, 2018 at 11:58

1 Answer 1

0

Maybe you need carriage return, or \r. This takes you to the beginning of the line. It is the same effect as in a physical typewriter when you move your carriage to the beginning and overwrite whatever is there.

If you use this:

print("Hello", end=" ")
print("world")

The output will be:

Hello World

But if you use:

print("Hello", end="\r")
print("world")

The output will be only:

world
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.