Reputation: 41
I wonder if there is some neat way to extract user input arguments from the input()
function and store them in a list
. Exactly like sys.argv
stores the command line arguments.
So for example, if the user inputs
square 4 "string with space" -arg
The list
of args should contain ['square', '4', 'string with space', '-arg']
.
Upvotes: 1
Views: 128
Reputation: 78780
The csv
module provides a way to split strings while keeping quoted substrings intact.
>>> import csv
>>> s = 'square 4 "string with space" -arg'
>>> next(csv.reader([s], delimiter=' '))
['square', '4', 'string with space', '-arg']
This is not proper command line parsing, of course. If you can take the user input upon program invocation, look into the argparse
module.
Upvotes: 1