How to Solve Python ValueError: empty separator

by | Programming, Python, Tips

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.


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:

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!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!