Last updated: Apr 8, 2024
Reading timeยท3 min
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.
my_str = 'bobby hadz com' a, b, c = my_str.split(' ') print(a) # ๐๏ธ bobby print(b) # ๐๏ธ hadz print(c) # ๐๏ธ com
The example splits the string into a list of strings on each space, but you could use any other delimiter.
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.
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
.
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
The str.split() method splits the string into a list of substrings using a delimiter.
The method takes the following 2 parameters:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At 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.
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(' ')
You can use an underscore if you want to discard one or more of the values.
my_str = 'bobby hadz com' a, b, _ = my_str.split(' ') print(a) # ๐๏ธ bobby print(b) # ๐๏ธ hadz
The underscore _
character is a convention that signals to the reader of the
code that the value is not needed.
If your string starts with or ends with the specific delimiter, you will get empty string elements in the list.
my_str = ' bobby hadz com ' # ๐๏ธ ['', 'bobby', 'hadz', 'com', ''] print(my_str.split(' '))
You can use the filter()
function to
remove any empty strings from the list.
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
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.
None
for the function argument, all falsy elements of the iterable are removed.filter()
function returns a filter
object, so we have to use the list()
class to convert the filter
object to a list.You can also pass any other separator to the str.split()
method. Here is an
example that uses a comma.
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 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.
my_str = 'bobby hadz com' print(my_str.split(',')) # ๐๏ธ ['bobby hadz com']
You can learn more about the related topics by checking out the following tutorials: