Split a String into multiple Variables in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# Split a string into multiple variables in Python

Unpack the values to split a string into multiple variables.

The str.split() method will split the string into a list of strings, which can be assigned to variables in a single declaration.

main.py
my_str = 'bobby hadz com' a, b, c = my_str.split(' ') print(a) # ๐Ÿ‘‰๏ธ bobby print(b) # ๐Ÿ‘‰๏ธ hadz print(c) # ๐Ÿ‘‰๏ธ com

split string into multiple variables

The code for this article is available on GitHub

The example splits the string into a list of strings on each space, but you could use any other delimiter.

main.py
my_str = 'bobby hadz com' # ๐Ÿ‘‡๏ธ ['bobby', 'hadz', 'com'] print(my_str.split(' '))

Make sure to declare exactly as many variables as there are items in the list.

If the variables on the left-hand side are more or fewer than there are items in the list, an error is raised.

# Using the maxsplit argument in the call to split()

The maxsplit argument can be used to set the number of splits.

For example, if you only want to split into two variables, set the maxsplit() argument to 1.

main.py
my_str = 'bobby hadz com' # ๐Ÿ‘‡๏ธ ['bobby', 'hadz com'] print(my_str.split(' ', 1)) # ๐Ÿ‘ˆ๏ธ split string maximum of 1 times a, b = my_str.split(' ', 1) print(a) # ๐Ÿ‘‰๏ธ bobby print(b) # ๐Ÿ‘‰๏ธ hadz com

using maxsplit argument when calling split

The code for this article is available on GitHub

The str.split() method splits the string into a list of substrings using a delimiter.

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)

If the separator is not found in the string, a list containing only 1 element is returned.

If you try to unpack more or fewer values than there are in the list, you would get an error.

main.py
my_str = 'bobby hadz com' print(my_str.split(' ')) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com'] # โ›”๏ธ ValueError: too many values to unpack (expected 2) a, b = my_str.split(' ')
We declare 2 variables, but the list contains 3 items. The inconsistency between the number of variables and items in the list causes the error.

# Using an underscore to discard values

You can use an underscore if you want to discard one or more of the values.

main.py
my_str = 'bobby hadz com' a, b, _ = my_str.split(' ') print(a) # ๐Ÿ‘‰๏ธ bobby print(b) # ๐Ÿ‘‰๏ธ hadz

using underscore to discard values

The code for this article is available on GitHub

The underscore _ character is a convention that signals to the reader of the code that the value is not needed.

# Leading and trailing delimiters

If your string starts with or ends with the specific delimiter, you will get empty string elements in the list.

main.py
my_str = ' bobby hadz com ' # ๐Ÿ‘‡๏ธ ['', 'bobby', 'hadz', 'com', ''] print(my_str.split(' '))

leading and trailing delimiters

You can use the filter() function to remove any empty strings from the list.

main.py
my_str = ' bobby hadz com ' # ๐Ÿ‘‡๏ธ ['', 'bobby', 'hadz', 'com', ''] print(list(filter(None, my_str.split(' ')))) a, b, k = list(filter(None, my_str.split(' '))) print(a) # ๐Ÿ‘‰๏ธ bobby print(b) # ๐Ÿ‘‰๏ธ hadz print(k) # ๐Ÿ‘‰๏ธ com

using filter function to remove leading and trailing delimiters

The code for this article is available on GitHub

The filter() function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.

If you pass None for the function argument, all falsy elements of the iterable are removed.
Note that the filter() function returns a filter object, so we have to use the list() class to convert the filter object to a list.

# Specifying the correct delimiter

You can also pass any other separator to the str.split() method. Here is an example that uses a comma.

main.py
my_str = 'bobby,hadz,com' # ๐Ÿ‘‡๏ธ ['', 'bobby', 'hadz', 'com', ''] print(my_str.split(',')) a, b, c = my_str.split(',') print(a) # ๐Ÿ‘‰๏ธ bobby print(b) # ๐Ÿ‘‰๏ธ hadz print(c) # ๐Ÿ‘‰๏ธ com
The code for this article is available on GitHub

The split() method takes a delimiter as an argument and splits the string on each occurrence of the delimiter, so make sure to specify the correct delimiter.

If the delimiter is not found in the string, a list containing only one item is returned.

main.py
my_str = 'bobby hadz com' print(my_str.split(',')) # ๐Ÿ‘‰๏ธ ['bobby hadz com']
Want to learn more about splitting strings in Python? Check out these resources: Split a String and remove the Whitespace in Python,Split a String and get First or Last element in Python.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev