If you pass an empty string to the str.split()
method, you will raise the ValueError: empty separator. If you want to split a string into characters you can use list comprehension or typecast the string to a list using list()
.
def split_str(word): return [ch for ch in word] my_str = 'Python' result = split_str(my_str) print(result)
This tutorial will go through the error in detail with code examples.
Table of contents
Python ValueError: empty separator
In Python, a value is information stored within a particular object. We will encounter a ValueError in Python when we use an operation or function that receives an argument with the right type but an inappropriate value.
The split()
method splits a string into a list. We can specify the separator, and the default is whitespace if we do not pass a value for the separator. In this example, an empty separator ""
is an inappropriate value for the str.split()
method.
Example #1: Split String into Characters
Let’s look at an example of trying to split a string into a list of its characters using the split()
method.
my_str = 'research' chars = my_str.split("") print(chars)
Let’s run the code to see what happens:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [7], in <cell line: 3>() 1 my_str = 'research' ----> 3 chars = my_str.split("") 5 print(chars) ValueError: empty separator
The error occurs because did not pass a separator to the split()
method.
Solution #1: Use list comprehension
We can split a string into a list of characters using list comprehension. Let’s look at the revised code:
my_str = 'research' chars = [ch for ch in my_str] print(chars)
Let’s run the code to get the list of characters:
['r', 'e', 's', 'e', 'a', 'r', 'c', 'h']
Solution #2: Convert string to a list
We can also convert a string to a list of characters using the built-in list()
method. Let’s look at the revised code:
my_str = 'research' chars = list(my_str) print(chars)
Let’s run the code to get the result:
['r', 'e', 's', 'e', 'a', 'r', 'c', 'h']
Example #2: Split String using a Separator
Let’s look at another example of splitting a string.
my_str = 'research is fun' list_of_str = my_str.split("") print(list_of_str)
In the above example, we want to split the string by the white space between each word. Let’s run the code to see what happens:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [10], in <cell line: 3>() 1 my_str = 'research.is.fun' ----> 3 list_of_str = my_str.split("") 5 print(list_of_str) ValueError: empty separator
The error occurs because ""
is an empty separator and does not represent white space.
Solution
We can solve the error by using the default value of the separator, which is white space. We need to call the split() method without specifying an argument to use the default separator. Let’s look at the revised code:
my_str = 'research is fun' list_of_str = my_str.split() print(list_of_str)
Let’s run the code to see the result:
['research', 'is', 'fun']
Summary
Congratulations on reading to the end of this tutorial!
For further reading on Python ValueErrors, go to the articles:
- How to Solve Python ValueError: year is out of range
- How to Solve Python ValueError: dictionary update sequence element #0 has length N; 2 is required
To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.