How to Split a String Into Multiple Variables Rather Than a List?

Problem Formulation

Recap: The Python string.split(separator) method splits the string at a given separator and returns a split list of substrings. Per default, it uses arbitrary whitespace as a separator.

Thus, if you want to store the result in a list variable, you can simply do so:

>>> my_string = 'learn python finxter'
>>> words = my_string.split()
>>> words
['learn', 'python', 'finxter']

However, what if you’d rather want to store the resulting words in individual variables than to store it in a list? In other words, you want the three variables a, b, and c to contain the words 'learn', 'python', and 'finxter'.

How can you split a string into multiple variables?

Method 1: Multiple Assignment

Python provides a feature called multiple assignments (also called iterable unpacking) that allows you to perform an n-to-n assignment operation by providing an iterable of values on the right-side of the assignment operator = and a combination of variables to assign them to.

To assign the result of a string.split() method to multiple variables, you can simply use comma-separated variables on the left side of the assignment operator = like so:

my_string = 'learn python finxter'
a, b, c = my_string.split()

The output is:

print(a)
# learn

print(b)
# python

print(c)
# finxter

However, this approach only works if split() returns the same number of elements as variables are provided. For example, the following code snippet will raise a ValueError:

my_string = 'learn python with finxter'
a, b, c = my_string.split()

Output:

Traceback (most recent call last):
  File "C:\Users\xcent\Desktop\code.py", line 2, in <module>
    a, b, c = my_string.split()
ValueError: too many values to unpack (expected 3)

Next, you’ll learn the most simple way to fix this issue.

Method 2: String Split with maxsplit Argument

The Python string.split() method provides an optional argument maxsplit that defines the maximal number of times the original string is split. The resulting iterable will have up to maxsplit+1 elements. Thus, if you need to assign the split result to n variables using multiple assignment without error, you need to set maxsplit=n-1.

my_string = 'learn python with finxter'
a, b, c = my_string.split(maxsplit=2)

print(a)
# learn

print(b)
# python

print(c)
# with finxter

While this is the simplest and most straightforward way to avoid the error, I’ll show you an alternative just for fun (and learning) next!

But first, in case you need a quick recap on the split() method, feel free to watch the following couple of seconds in this explainer video:

Method 3: Multiple Assignment with Asterisk Throw-Away Variable *_

Per convention, you can use the throw-away single underscore _ as a throw-away variable. When combined with an asterisk *_, the unpacking operator, you can store all unneeded words in the single underscore variable and simply ignore them. If the string has only three words, those words are stored in the variables a, b, c—but if the string has more words, all remaining words are then stored in _ and no error will be raised!

my_string = 'learn python with finxter'
a, b, *_, c = my_string.split()

If you run the code snippet with four words, Python simply ignores the additional word 'with' by storing it in the throw-away variable.

Output:

print(a)
# learn

print(b)
# python

print(c)
# finxter

print(_)
# ['with']

You can learn more about the asterisk operator in the following video:

Feel free to also check out our in-depth guide on the unpacking operator.

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!