0

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.

5
  • 2
    What have you tried so far? Commented Mar 15, 2021 at 18:10
  • I've tried using arrays, but I'm not good at all at advanced python functions. I've read some documentation on lists and tuples, but I'm not sure how to use them. Commented Mar 15, 2021 at 18:20
  • Read about split(), looks like a good fit for your problem. Commented Mar 15, 2021 at 18:21
  • I just did a split function and it works perfectly. Thanks! Commented Mar 15, 2021 at 18:26
  • I forgot to add, but how do you then divide up the split into variables? Commented Mar 15, 2021 at 18:35

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.