TheAlgorithms/Python · error · ValueError

the string should be not empty string

Error message

the string should be not empty string

What it means

Raised by word_break() when the target string is not a str instance or is empty. The algorithm builds a trie character-by-character over the input, so an empty string leaves nothing to match and non-string input would break iteration. This check runs before the words validation.

Source

Thrown at dynamic_programming/word_break.py:59

        ...
    ValueError: the string should be not empty string
    >>> word_break('', ['a'])
    Traceback (most recent call last):
        ...
    ValueError: the string should be not empty string
    >>> word_break('abc', [123])
    Traceback (most recent call last):
        ...
    ValueError: the words should be a list of non-empty strings
    >>> word_break('abc', [''])
    Traceback (most recent call last):
        ...
    ValueError: the words should be a list of non-empty strings
    """

    # Validation
    if not isinstance(string, str) or len(string) == 0:
        raise ValueError("the string should be not empty string")

    if not isinstance(words, list) or not all(
        isinstance(item, str) and len(item) > 0 for item in words
    ):
        raise ValueError("the words should be a list of non-empty strings")

    # Build trie
    trie: dict[str, Any] = {}
    word_keeper_key = "WORD_KEEPER"

    for word in words:
        trie_node = trie
        for c in word:
            if c not in trie_node:
                trie_node[c] = {}

            trie_node = trie_node[c]

View on GitHub (pinned to f5988cc097)

Solutions

  1. Guard the call: if not string: skip or handle the empty case in your caller.
  2. Ensure the value is str: decode bytes with .decode('utf-8') and coerce with str() only when appropriate.
  3. Treat an empty target as a no-op (trivially breakable) rather than an error in your own flow.

Example fix

# before
word_break('', ['a', 'b'])  # ValueError

# after
result = True if not string else word_break(string, ['a', 'b'])
Defensive patterns

Strategy: validation

Validate before calling

def valid_target(string: object) -> bool:
    return isinstance(string, str) and len(string) > 0

Type guard

def non_empty_str(s: object) -> TypeGuard[str]:
    return isinstance(s, str) and len(s) > 0

Try / catch

try:
    word_break(string, words)
except ValueError as e:
    if 'not empty string' in str(e):
        return True  # empty string trivially breaks
    raise

Prevention

When it happens

Trigger: Calling word_break('', words) or word_break(None, words) or word_break(123, ['1','2']). A string of only spaces (' ') does NOT trigger it — only zero length or non-str types do.

Common situations: Passing a stripped/filtered value that became empty (e.g. blank line from a file); a variable that is None after a failed parse; feeding bytes instead of str in Python 3.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/948376682d6fbc42. Report an issue: GitHub.