TheAlgorithms/Python · error · ValueError

Empty string was passed to the function

Error message

Empty string was passed to the function

What it means

Raised by octal_to_binary(octal_number) in conversions/octal_to_binary.py:33 when the input string is falsy — empty ('') or None. Unlike sibling functions in this repo it does not strip whitespace first, so a blank-but-non-empty string like ' ' instead fails the digit check (' ' is not in '01234567'). Empty input means no digits to expand into 3-bit groups, hence ValueError('Empty string was passed to the function').

Source

Thrown at conversions/octal_to_binary.py:33

    >>> octal_to_binary("17")
    '001111'
    >>> octal_to_binary("7")
    '111'
    >>> octal_to_binary("Av")
    Traceback (most recent call last):
        ...
    ValueError: Non-octal value was passed to the function
    >>> octal_to_binary("@#")
    Traceback (most recent call last):
        ...
    ValueError: Non-octal value was passed to the function
    >>> octal_to_binary("")
    Traceback (most recent call last):
        ...
    ValueError: Empty string was passed to the function
    """
    if not octal_number:
        raise ValueError("Empty string was passed to the function")

    binary_number = ""
    octal_digits = "01234567"
    for digit in octal_number:
        if digit not in octal_digits:
            raise ValueError("Non-octal value was passed to the function")

        binary_digit = ""
        value = int(digit)
        for _ in range(3):
            binary_digit = str(value % 2) + binary_digit
            value //= 2
        binary_number += binary_digit

    return binary_number


if __name__ == "__main__":

View on GitHub (pinned to f5988cc097)

Solutions

  1. Skip or handle empty tokens before calling: if not octal_number: continue
  2. Strip and default: octal_number = (octal_number or '').strip() or '0'
  3. Avoid splitting with keepempty behavior: [t for t in s.split(',') if t.strip()]

Example fix

# before
tokens = '755,,644'.split(',')
for t in tokens:
    octal_to_binary(t)  # '' -> ValueError

# after
for t in '755,,644'.split(','):
    if t.strip():
        octal_to_binary(t)
Defensive patterns

Strategy: validation

Validate before calling

octal_number = (octal_number or '').strip()
if not octal_number:
    raise ValueError('octal string is required')

Type guard

def is_nonempty_octal_input(v: object) -> TypeGuard[str]:
    return isinstance(v, str) and bool(v.strip())

Prevention

When it happens

Trigger: octal_to_binary(''), octal_to_binary(None) (falsy), or a loop over tokens where one token is empty.

Common situations: Splitting input on a delimiter that yields empty fields (trailing separators); optional parameters defaulting to '' or None; reading columns from a data file with missing values.

Related errors


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