0

I'm wondering if there is a way to be able to print a statement after the input on the same line.

Like how print() has 'end=', but input does not...

user = input("Type Here: ")
print("Text")

# -- Output:
# Type Here: input_here
# text_here

# -- Wanted Output:
# Type Here: input_here text_here
  • Welcome to SO. Already answered here: https://stackoverflow.com/questions/52490109/python-how-do-i-put-print-statement-and-input-on-same-line – Mohammad-Reza Malekpour Jan 03 '23 at 08:02
  • Damn SO is rough, didn't see the comments and got wrecked in 2 seconds. that's enough SO for the day. – Sin Han Jinn Jan 03 '23 at 08:06
  • For answering the question a more narrow sense, see https://stackoverflow.com/questions/12586601/remove-last-stdout-line-in-python and answers therein. In short, what you want requires some hacky terminal control sequences/using curses. – Lodinn Jan 03 '23 at 08:07

3 Answers3

1

The input() function is not very fancy. And it will advance the cursor down to start of next line when the user hits RETURN.

But you can overwrite what happened on that line by sending an ANSI escape sequence:

up = chr(27) + "[A"

For example:

name = input("Name? ")
print(up + "Name is " + name + "    Pleased to meet you.    ")

For fancier approaches, you will need a library like curses or GNU readline.

J_H
  • 17,926
  • 4
  • 24
  • 44
  • Note that all this does is go back to leftmost character of the previous line, not what the OP asked for where the cursor is placed at the end of previous line – Samathingamajig Jan 03 '23 at 08:12
  • OP knows three things: prompt, response, extra_text. By prepending `up`, OP is in a position to overwrite original prompt with same prompt, overwrite response with same response, and then continue writing any extra_text that is desired. Yes, there are ANSI escape sequences for sending cursor to certain X,Y location, but that seems more complex for OP to manage, and less reliable in the face of window resize events. The proposed technique continues to offer robust output even in edge cases where stdout is redirected to a file. – J_H Jan 03 '23 at 08:16
1

Go up a line, then forward the length of the input. Note that this doesn't work if there was already text on the current line before calling noline_input

def noline_input(prompt):
    data = input(prompt)
    print(end=f"\033[F\033[{len(prompt)+len(data)+1}G") # or +2 if you want an extra space
    return data
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34
0

You can create your own version of the input function by getting keystrokes silently with the msvcrt.getch function in a loop. With each keystroke, you either append it to a list and output it to the console, or abort the loop if it's a carriage return.

To deal with a backspace, pop the last character from the list and output a backspace, then a space to erase the last character from the console, and then another backspace to actually move the cursor backwards.

Note that msvcrt.getch works only in Windows, and you should install the getch package instead in other platforms:

try:
    from msvcrt import getch
except ModuleNotFoundError:
    from getch import getch

def input_no_newline(prompt=''):
    chars = []
    print(prompt, end='', flush=True)
    while True:
        char = getch().decode()
        if char == '\r':
            break
        if char != '\b':
            chars.append(char)
            print(char, end='', flush=True)
        elif chars:
            chars.pop()
            print('\b \b', end='', flush=True)
    return ''.join(chars)

user = input_no_newline("Type Here: ")
print("Text")
blhsing
  • 91,368
  • 6
  • 71
  • 106