Bohdan
Bohdan

Reputation: 581

Advanced input in python

I want to receive some information from a user in a next way:

My score is of 10 - is already printed

Between 'is' and 'of' there is an empty place for user's input so he doesn't enter his information at the end( if using simple input() ) but in the middle. While user is entering some information it appears between 'is' and 'of'

Is there any possible way to do it?

Upvotes: 6

Views: 2290

Answers (2)

chepner
chepner

Reputation: 531075

One way to get something close to what you want is if you terminal supports ANSI escape codes:

x = input("My score is \x1b[s  of 10\x1b[u")

\x1b is the escape character. Neither escape character is dipsplayed on the screen; instead, they introduce byte sequences that the terminal interprets as an instruction of some kind. ESC[s tells the terminal to remember where the cursor is at the moment. ESC[u tells the terminal to move the cursor to the last-remembered position.

enter image description here

(The rectangle is the cursor in an unfocused window.)

Using a library that abstracts away the exact terminal you are using is preferable, but this gives you an idea of how such libraries interact with your terminal: it's all just bytes written to standard output.

Upvotes: 3

Usmiech
Usmiech

Reputation: 301

If you use console then consider importing curses library. It works on both linux and windows. Download it for windows from http://www.lfd.uci.edu/~gohlke/pythonlibs/#curses

With this library you have a total control over console. Here is the answer to your question. How to input a word in ncurses screen?

Upvotes: 2

Related Questions