pythonnovice
pythonnovice

Reputation: 3

How to Format User Inputs Into Variables in Python

I'm trying to format a user input into variables. I'm a novice to most advanced functions in python, but here's an example prompt:

userInput = input("Enter your four-character list:")

The user is supposed to enter three or four characters, a mix of letters and numbers. I would like to slice up the input into variables that can be plugged into other functions. For example, if the user input is "4 5 s t," I would like each of the four characters to be a variable. Spacing also shouldn't matter, whether it's at least one or five between each.

Upvotes: 0

Views: 321

Answers (1)

Christos
Christos

Reputation: 26

You can use the split method of strings in python

my_args = userInput.split()

This will return a python list of the input elements regardless of how many spaces there are.

Then you can work with your list like this

for arg in my_args:
    #do something

Upvotes: 1

Related Questions