I have a "form" where I have a few raw_input
s to get the user response.
Now I want them to be pre-initialized with a default value. Is there any way to fill these raw_input
fields? Or is there a good alternative to raw_input
where that is possible?
To clarify, I have something looking like this:
val = raw_input("Input val:")
What I want as output is the following:
Input val: default value
and I want the user to be able to erase or edit the default value as if they had written it themselves. Is there any way to do that?
raw_input
is not like a web form. The name should give it away: all the function does is accept raw input (in the form of a string), optionally printing a prompt first. If you want a default value, you'll need to set it yourself as in Elizion's answer.
Now, that said, you can hack the input prompt to look sort of like what you want. I do not recommend this at all, but for the sake of completeness, give the code below a try in an interactive console:
default_val = 'default'
i = raw_input('Input val: {}\rInput val: '.format(default_val)) or default_val
Here I'm abusing the \r
character to move the cursor back to the start of the prompt. Then I re-write the actual prompt part, leaving the cursor on the d
character, so that the user can type "over" it.
You'll notice though that "default"
is still displayed there until you've typed over all of it (at which point it's probably gone for good, depending on your console's behavior). But it's only displayed; not actually part of the input. If the user enters "foo"
(which will make the prompt look like this: Input val: fooult
), i
will get the value of "foo"
. If the user just presses Enter without entering text, i
will be the empty string, which is why the or default_val
is still necessary in the code. All this is to say that this is almost certainly not what you want to do. If you want a web form, make a web form. raw_input
(and for that matter, console I/O) was not made for this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With