How to enter multiple strings via input()
11,613
No, input()
only allows one string input to be supplied.
What you can do is this:
name, age, gender = input('Enter Name|Age|Gender:').split('|')
# user inputs "ABC|30|M"
print(name, age, gender)
# ABC 30 M
Now you just rely on user not having a |
character in their name.
Or, of course, you can ask separate questions.
Author by
Pedro Serrano
Updated on June 13, 2022Comments
-
Pedro Serrano over 1 year
When using raw_input in python, a user has to put an input and then press enter. Is there a way for me to code something, where the code will prompt several user inputs at the same time and THEN press enter for the code to run?
For example: instead of ...
>>> Name: <prompt> <enter> >>> Age: <prompt> <enter> >>> Gender: <prompt> <enter>
it'll have ...
>>> Name: <prompt> Age: <prompt> Gender: <prompt> <enter>
-
Alex over 5 yearsYou may want to alter the way you ingest data entirely if you're running a script with multiple parameters or options. The
argparse
package is good for passing options to a script using command line flags. You could also leverage something like thejson
orconfigparser
modules to ingest these paramters from a file. -
Håken Lid over 5 years
input
is meant to be very simple and minimal. If you want more advanced user interaction, there are many ways to create a user interface, depending on your target environment. Do you want a text terminal application, a graphical user interface or maybe a web application? -
jpp over 5 years@PedroSerrano, if one of the below solutions helped, consider accepting it (green tick on left).
-