Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show default value for editing on Python input possible?

Tags:

python

input

Is it possible for python to accept input like this:

 Folder name: Download 

But instead of the user typing "Download" it is already there as a initial value. If the user wants to edit it as "Downloads" all he has to do is add a 's' and press enter.

Using normal input command:

folder=input('Folder name: ') 

all I can get is a blank prompt:

 Folder name: 

Is there a simple way to do this that I'm missing?

like image 447
kircheis Avatar asked Mar 28 '10 13:03

kircheis


People also ask

How do you show input value in Python?

In Python, Using the input() function, we take input from a user, and using the print() function, we display output on the screen. Using the input() function, users can give any information to the application in the strings or numbers format.

How do you manipulate inputs in Python?

We can use the raw_input() function in Python 2 and the input() function in Python 3. By default the input function takes an input in string format. For other data type you have to cast the user input. In Python 3 we use the input() function which returns a user input value.

Is Python input string by default?

By default, inputs are stored as 'strings'. Therefore, we can use the map() function to indicate that the input will be converted into integers and then their values are stored in a variable.

What is the default data type for input value in Python?

Assuming you're using python 3. X, everything the user inputs will be a string.


1 Answers

The standard library functions input() and raw_input() don't have this functionality. If you're using Linux you can use the readline module to define an input function that uses a prefill value and advanced line editing:

import readline  def rlinput(prompt, prefill=''):    readline.set_startup_hook(lambda: readline.insert_text(prefill))    try:       return input(prompt)  # or raw_input in Python 2    finally:       readline.set_startup_hook() 
like image 159
sth Avatar answered Sep 30 '22 01:09

sth