user18427332
user18427332

Reputation:

Is it possible to create an input with an editable default value?

I am a python newbie and currently fiddling with it in various ways.

But I'm kind of stuck at creating an input with editable default value.

For example,

if you run input, there will be default value which you can change or leave it be.

Is it possible to create such input with standard library?

Upvotes: 3

Views: 2079

Answers (3)

Freddy Mcloughlan
Freddy Mcloughlan

Reputation: 4506

Using input

default = "foo"

while default != "quit":
    default = input(f"Enter a value ({default}): ")

Where you need to click enter each time, and quit by typing quit

Upvotes: -1

buran
buran

Reputation: 14253

default = 'spam'
user_input = input(f"Enter a string (default: {default}):") or default
print(user_input)

output:

Enter a string (default: spam): # user hit Enter
spam

Note, this assumes user input will never be empty string (i.e. empty string is not valid input for your use case).

Upvotes: 0

Ukulele
Ukulele

Reputation: 695

You can test whether the user has inputted anything then if they haven't replace it:

default_val = 'Default answer'
inp = input("Enter a string (default value is: '"+default_val+"'): ")
if not inp:
    inp = default_val
print(inp)

This outputs:

Enter a string (default value is: 'Default answer'): 
Default answer

or

Enter a string (default value is: 'Default answer'): Different answer
Different answer

Upvotes: 1

Related Questions