This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Pre-populate user editable text in input()
Type: enhancement Stage:
Components: Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: steven.daprano, terry.reedy
Priority: normal Keywords:

Created on 2019-06-05 10:40 by steven.daprano, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg344701 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-06-05 10:40
When asking for user input, it is often very helpful to be able to pre-populate the user's input string and allow them to edit it, rather than expecting them to re-type the input from scratch.

I propose that the input() built-in take a second optional argument, defaulting to the empty string. The first argument remains the prompt, the second argument is the initial value of the editable text.

Example use-case:

    pathname = "~/Documents/untitled.txt"
    pathname = input("Save the file as...? ", pathname)


On POSIX systems, this can be done with readline:


import readline
def myinput(prompt, initial=''):
    readline.set_startup_hook(lambda: readline.insert_text(initial))
    try:
        response = input(prompt)
    finally:
        readline.set_startup_hook(None)
    return response


but it requires readline (doesn't exist on Windows), and it clobbers any pre-existing startup hook the caller may have already installed.
msg345012 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-06-07 22:51
I like the idea, but doubt this can be implemented.  Python assumes a least-common-denominator dumb terminal.  Program output is sent on stdout/err and displayed read-only.  Used input is read from stdin.  It is put there either key by key or after the Enter key is hit.  An input buffer allows editing before hitting enter.

'Dumb' means 'No smart escape code to put following output into the user input buffer.' Actually, I don't know that there is an ansi codes to do that.  I am curious how readline.insert_text works.  (I don't have it on Windows.)

Even if the feature could be made to run on Mac/Linux/Windows10 system consoles with custom , there is the issue of supporting other displays, such as IDLE.
History
Date User Action Args
2022-04-11 14:59:16adminsetgithub: 81342
2019-06-07 22:51:52terry.reedysetnosy: + terry.reedy
messages: + msg345012
2019-06-05 10:40:49steven.dapranocreate