6

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?

Bohdan
  • 424
  • 1
  • 4
  • 15

2 Answers2

3

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.

chepner
  • 497,756
  • 71
  • 530
  • 681
2

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?

Community
  • 1
  • 1
Usmiech
  • 181
  • 3
  • 12
  • 1
    The other way would be far more complicated; I accomplished it a few years ago, I dont remember the code, but I used carriage return ( \r ) and constantly refreshed the console. – Usmiech Feb 11 '17 at 13:12
  • So far this is the only answer that appears to have come from someone who took the trouble to read the question. – Mad Physicist Feb 11 '17 at 13:35
  • @Usmiech See my answer for one possible example. – chepner Feb 11 '17 at 13:53