Borislav Hadzhiev
Last updated: Jun 24, 2022
Check out my new book
Use the str.split()
method to split a string by comma, e.g.
my_list = my_str.split(',')
. The str.split
method will split the string on
each occurrence of a comma and will return a list containing the results.
# ✅ split string on each occurrence of comma my_str = 'one,two,three,four' my_list = my_str.split(',') print(my_list) # 👉️ ['one', 'two', 'three', 'four'] # ✅ split string on each space or comma my_str_2 = 'one two,three four five' my_list_2 = my_str_2.replace(',', ' ').split(' ') print(my_list_2) # 👉️ ['one', 'two', 'three', 'four', 'five']
The str.split() method splits the original 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.
my_str = 'one' my_list = my_str.split(',') # 👇️ ['one'] print(my_list)
If you have whitespace between the comma-separated words in the string and need
to remove it, use the str.strip()
method.
my_str = 'one , two , three ,four' my_list = [item.strip() for item in my_str.split(',')] print(my_list) # 👉️ ['one', 'two', 'three', 'four']
We used a list comprehension to remove the leading and trailing whitespace from each string.
The str.strip method returns a copy of the string with the leading and trailing whitespace removed.
If your string starts with or ends with a comma, you would get empty string elements in the list.
my_str = ',one,two,three,four,' my_list = my_str.split(',') print(my_list) # 👉️ ['', 'one', 'two', 'three', 'four', '']
You can use the filter()
function to remove any empty strings from the list.
my_str = ',one,two,three,four,' my_list = list(filter(None, my_str.split(','))) print(my_list) # 👉️ ['one', 'two', 'three', 'four']
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.All values that are not truthy are considered falsy. The falsy values in Python are:
None
and False
.0
(zero) of any numeric type""
(empty string), ()
(empty tuple), []
(empty list), {}
(empty dictionary), set()
(empty set), range(0)
(empty
range).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.
my_str = 'one,two,three,four' my_str_2 = 'one two,three four five' my_list_2 = my_str_2.replace(',', ' ').split(' ') print(my_list_2) # 👉️ ['one', 'two', 'three', 'four', 'five']
We replaced all occurrences of a comma with a space and split the string on each space.
You could achieve the same result by replacing each occurrence of a space with a comma and splitting on each comma.
my_str = 'one,two,three,four' my_str_2 = 'one two,three four five' my_list_2 = my_str_2.replace(' ', ',').split(',') print(my_list_2) # 👉️ ['one', 'two', 'three', 'four', 'five']