0

I wish to separate a string that looks like this: 'O,t' 'R,p' 'A/d' etc. The string will be a user's input, therefore I wish to use the join function and unpacking.

Which separator should I use? Obviously this doesn't work because there are no spaces:

user_input = 'O,t'
a,b,c = user_input.split('')

# Output should be:
# a = 'O', b = ',' , c = 't'

I want to avoid using list(user_input) if possible.

azro
  • 53,056
  • 7
  • 34
  • 70
tibetish
  • 109
  • 8
  • 1
    What about `a, b, c = user_input` ? – khelwood Dec 31 '19 at 14:17
  • 2
    Why do you want to avoid the obvious and correct way of doing it? You can use `[c for c in user_input]` too, but that's just being obtuse and verbose for no good reason. – Martijn Pieters Dec 31 '19 at 14:17
  • 1
    and khelwood is correct, if all you are doing is assigning to separate variables, then just use `a, b, c = user_input`. Any iterable will do to unpack, and strings are iterables. – Martijn Pieters Dec 31 '19 at 14:20
  • Why do you want to avoid using `list`? – juanpa.arrivillaga Dec 31 '19 at 14:22
  • "but that's just being obtuse and verbose" :) Well, no. That's just being new to python. I didn't realize the join is actually unnecessary here. Thanks for your help. I now understand where I went wrong – tibetish Dec 31 '19 at 14:27
  • A better duplicate target: [How to split a string into characters and assign each character to a separate variable](https://stackoverflow.com/q/47501324/7851470) – Georgy Jun 21 '20 at 13:54

0 Answers0