This is largely environment-dependent.
The following is a windows-only solution:
from ctypes import *
from ctypes import wintypes
def get_stderr_handle():
# stdin handle is -10
# stdout handle is -11
# stderr handle is -12
return windll.kernel32.GetStdHandle(-12)
def get_console_screen_buffer_info():
csbi_ = CONSOLE_SCREEN_BUFFER_INFO()
windll.kernel32.GetConsoleScreenBufferInfo(get_stderr_handle(), byref(csbi_))
return csbi_
class CONSOLE_SCREEN_BUFFER_INFO(Structure):
"""struct in wincon.h."""
_fields_ = [
("dwSize", wintypes._COORD),
("dwCursorPosition", wintypes._COORD),
("wAttributes", wintypes.WORD),
("srWindow", wintypes.SMALL_RECT),
("dwMaximumWindowSize", wintypes._COORD),
]
csbi = get_console_screen_buffer_info()
first_input = input("enter first input: ")
cursor_pos = csbi.dwCursorPosition
cursor_pos.X = len("enter first input: ") + len(first_input) + 1
windll.kernel32.SetConsoleCursorPosition(get_stderr_handle(), cursor_pos)
second_input = input("enter second input: ")
The following is a linux solution, which uses backspace characters. There are some implementations of get_terminal_size()
here if you're using an older python version.
from shutil import get_terminal_size
first_input = input("enter first input: ")
second_input = input("\b"*(get_terminal_size()[0] - len("enter first input: ") - len(first_input) - 1) + "enter second input: ")