[New-bugs-announce] [issue37161] Pre-populate user editable text in input()

Steven D'Aprano report at bugs.python.org
Wed Jun 5 06:40:49 EDT 2019


New submission from Steven D'Aprano <steve+python at pearwood.info>:

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.

----------
messages: 344701
nosy: steven.daprano
priority: normal
severity: normal
status: open
title: Pre-populate user editable text in input()
type: enhancement
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37161>
_______________________________________


More information about the New-bugs-announce mailing list