1

The code:

input("Type your input here:)

displays as:

Type your input here:

I want to automatically populate the input window with text that can be cleared by pressing backspace so the display looks like:

Type your input here: DEFAULT

and after pressing backspace 3 times the user would see:

Type your input here: DEFA

Other posts have indicated that this isn't something you can do in, say, bash, but is there a way to do this in Python?

3
  • 1
    Which OS are you using? In the case of Linux please check that this helps: stackoverflow.com/questions/2533120/… Commented Mar 12, 2021 at 6:00
  • 1
    As @HetalThaker suggested a post, here is something for windows similar to that. Hope it helps:) Commented Mar 12, 2021 at 6:44
  • Thanks @YashvanderBamel looks like this is what I was looking for. Should have looked harder. I'll close this question Commented Mar 12, 2021 at 6:59

2 Answers 2

1

Simple answer: No. It's just not something you can do in a console app. What's commonly done is to display the default you'll get if you press return:

Type your input here [DEFAULT]: 
Sign up to request clarification or add additional context in comments.

Comments

0

If you want a fully featured terminal interface, curses is the way to go: https://docs.python.org/3/library/curses.html

For this, though, readline is pretty good: https://docs.python.org/3/library/readline.html

It should be noted that, despite being in the standard library, it might not be available on some Windows Python installs (see python's readline module not available for windows?).

import readline

def setup(text):
    readline.insert_text(text)
    readline.redisplay()

readline.set_pre_input_hook(lambda: setup("DEFAULT"))
a = input("Type your input here: ")

print(f"\n\"{a}\"\n")

readline.set_pre_input_hook(lambda: setup("OTHER DEFAULT"))
a = input("Type your input here: ")

print(f"\n\"{a}\"")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.