ValueError: empty separator (Fix)
This page helps beginners fix the Python error ValueError: empty separator. It explains why it happens, shows a small example that causes it, and gives simple ways to solve it.
Quick fix
text = "apple,banana,orange"
parts = text.split(",") # use a real separator
print(parts)
words = "one two three".split() # no argument splits on whitespace
print(words)
Output:
['apple', 'banana', 'orange']
['one', 'two', 'three']
Do not pass an empty string to split(). Use a real separator like "," or leave the argument out to split on whitespace.
What this error means
ValueError: empty separator means Python was asked to split text using an empty string.
This most often happens with:
text.split("")
Python does not allow an empty separator for str.split().
Example that causes the error
A common failing example is:
text = "hello"
parts = text.split("")
print(parts)
Output:
ValueError: empty separator
The problem is the empty string inside split("").
Why it happens
A separator must be at least one character long.
Python needs a real boundary such as:
- a space:
" " - a comma:
"," - a dash:
"-"
An empty string "" does not give Python a valid place to split.
How to fix it
Use a real separator
If your text uses commas, spaces, or another character between values, pass that character to split().
text = "red,green,blue"
colors = text.split(",")
print(colors)
Output:
['red', 'green', 'blue']
Use split() with no argument for whitespace
If you want to split words in a sentence, do not use " " unless you specifically need a single-space separator.
Use:
text = "one two three"
words = text.split()
print(words)
Output:
['one', 'two', 'three']
This is the usual solution for splitting text into words. You can learn more in how to split a string in Python.
Use list(text) for individual characters
Some beginners expect split("") to return each character. That is not how split() works.
If you want characters, use list(text):
text = "hello"
chars = list(text)
print(chars)
Output:
['h', 'e', 'l', 'l', 'o']
Choose the right fix for your goal
Use the fix that matches what you want to do:
- To split words in a sentence, use
split() - To split comma-separated values, use
split(",") - To turn a string into characters, use
list(text)
Examples:
sentence = "Python is fun"
print(sentence.split())
csv_text = "a,b,c"
print(csv_text.split(","))
text = "cat"
print(list(text))
Output:
['Python', 'is', 'fun']
['a', 'b', 'c']
['c', 'a', 't']
Debugging steps
If the error comes from a larger program, use these steps:
- Find the line that calls
split() - Check the value passed as the separator
- Print the separator if it comes from a variable
- Replace
""with a real separator or remove the argument
Useful debugging commands:
print(separator)
print(repr(separator))
print(type(separator))
print(text)
A full example:
text = "a-b-c"
separator = ""
print(separator)
print(repr(separator))
print(type(separator))
print(text)
parts = text.split(separator)
Output before the error:
''
<class 'str'>
a-b-c
repr(separator) is especially helpful because it makes the empty string easier to see.
Related errors and confusion
This error is different from other common string problems.
- It is not
ValueError: substring not found, which happens in different string operations - It is also different from a
TypeError, which happens when you pass the wrong data type - Beginners often confuse
split("")withlist(text)
If you want a broader overview of this kind of error, see ValueError in Python: causes and fixes.
Common mistakes
These are the most common causes of this error:
- Using
text.split("")directly - Passing a variable that contains an empty string into
split() - Expecting
split("")to return individual characters - Building the separator dynamically and ending up with
""
Example with a variable:
text = "apple|banana|orange"
separator = ""
parts = text.split(separator) # causes ValueError
print(parts)
To fix it, make sure separator is not empty:
text = "apple|banana|orange"
separator = "|"
parts = text.split(separator)
print(parts)
Output:
['apple', 'banana', 'orange']
FAQ
Why does split("") fail in Python?
Because Python does not allow an empty string as a separator. The separator must contain at least one character.
How do I split a string into characters?
Use list(text). That returns each character as a separate item.
How do I split by spaces?
Use split() with no argument. It splits on whitespace.
Can I use a variable as the separator?
Yes, but make sure the variable is not an empty string.