0

I want to get input and then print the output on that same line. This attempt doesn't work.

while True:
    op = input("Input: ")
    print("\tOutput: " + op)

The output looks like this:

Input: a
    Output:a
Input: cat
    Output cat
Input: 32
    Output: 32

I would like it to look like this:

Input: a     Output: a
Input cat    Output: cat
Input: 32    Output: 32

Is this possible in Python? It would require overwriting a line of output, but I don't know whether it's possible, or how to do it.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • 1
    it's not as easy as you might think, short of clearing the previous line and writing over it, more here http://stackoverflow.com/questions/7173850/possible-to-get-user-input-without-inserting-a-new-line – brennan Apr 07 '17 at 22:35

4 Answers4

1

You can't do it very easily, especially cross-platform. That's because when you press "enter" it's (surprise!) a newline.

So what you're going to have to do is move the cursor up a line. You can do this with control codes on linux - I'm not sure how to do it on Windows but it's probably awkward.

If you're on Mac/Linux, try this:

move_back = '\033[F'

choice = ''
while choice != 'q':
    choice = input('Input: ')
    print('{move_back}Input: {input_} Ouput: {output}'.format(
        move_back=move_back,
        input_=choice,
        output=choice.upper(),
    ))

The reason that you need to re-print your input is because the cursor starts at the beginning of the previous line. If you just print Output:... then it will overwrite what was already there. There are control codes to move the cursor, but it's just as easy to print out what you already have.

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
1

This depends on the binding for stdout and stdin (the default output and input channels). The crux of the problem is that the user has to hit "Enter" to submit the input, and that is usually bound to a CR/LF pair. If your I/O device is enabled for moving back one line, you can print that character, reprint the input, and then print the output.

Without knowing the device or its characteristics, I can't give actual code. Check your friendly neighborhood documentation for the appropriate control characters.

Prune
  • 76,765
  • 14
  • 60
  • 81
1

If you're on Windows, you can use Python's msvcrt module:

In [4]: import msvcrt
   ...: import sys
   ...: while True:
   ...:     op = ""
   ...:     print('Input: ', end='')
   ...:     while True:
   ...:         c = msvcrt.getch().decode('utf-8')  # capture single character
   ...:         if c == '\r':                       # enter pressed
   ...:             break
   ...:         op += c
   ...:         print(c, end='')                    # print character to stdout
   ...:     print('\tOutput: ' + op)                # print whole output
   ...:
Input: a        Output: a
Input: cat      Output: cat
Input: 32       Output: 32

Be careful when trying out this (or any other suggestions). If you're using an IDE, it's possible that these commands don't work in the console of the IDE. Try running the code in a standard Windows cmd.exe console and you should be fine.

Felix
  • 6,131
  • 4
  • 24
  • 44
-1
import **sys**
sys.stdout.write() #use

You Can use "+" To concat String because The write function takes one argument

to end any line using this function you will use "\n"

Neil
  • 14,063
  • 3
  • 30
  • 51
  • 2
    I think you misunderstand the question: OP wants to accept the input, then display the output (after processing) on the *same line* as that input. You haven't addressed the problem of the line feed / return. – Prune Apr 07 '17 at 22:33