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_hex() in conversions/octal_to_hexadecimal.py when the input is empty after removing an optional '0o' prefix. The function checks octal == "" after stripping the prefix, so both "" and the literal "0o" trigger it. It guards against trying to iterate over zero digits.

Source

Thrown at conversions/octal_to_hexadecimal.py:29

    Traceback (most recent call last):
        ...
    TypeError: Expected a string as input
    >>> octal_to_hex("Av")
    Traceback (most recent call last):
        ...
    ValueError: Not a Valid Octal Number
    >>> octal_to_hex("")
    Traceback (most recent call last):
        ...
    ValueError: Empty string was passed to the function
    """

    if not isinstance(octal, str):
        raise TypeError("Expected a string as input")
    if octal.startswith("0o"):
        octal = octal[2:]
    if octal == "":
        raise ValueError("Empty string was passed to the function")
    if any(char not in "01234567" for char in octal):
        raise ValueError("Not a Valid Octal Number")

    decimal = 0
    for char in octal:
        decimal <<= 3
        decimal |= int(char)

    hex_char = "0123456789ABCDEF"

    revhex = ""
    while decimal:
        revhex += hex_char[decimal & 15]
        decimal >>= 4

    return "0x" + revhex[::-1]

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check for empty/whitespace input before calling: if not octal or octal.strip('o0') == '' ... reject.
  2. Treat missing values as an error or default (e.g. '0') at the input boundary, not inside the conversion call.
  3. Validate required fields in the form/API layer before invoking the converter.

Example fix

# before
octal_to_hex("")  # ValueError: Empty string was passed to the function

# after
if not octal or octal.removeprefix('0o') == '':
    raise ValueError('octal input is required')
octal_to_hex(octal)
Defensive patterns

Strategy: validation

Validate before calling

if not isinstance(octal, str) or not octal.removeprefix('0o'):
    raise ValueError('octal value is required')
octal_to_hex(octal)

Type guard

def has_octal_payload(octal: object) -> bool:
    return isinstance(octal, str) and len(octal.removeprefix('0o')) > 0

Try / catch

try:
    octal_to_hex(octal)
except ValueError as e:
    if 'Empty string' in str(e):
        octal = '0'  # or prompt the user again
    else:
        raise

Prevention

When it happens

Trigger: Calling octal_to_hex("") or octal_to_hex("0o"). Also reached when a string variable that was initialized empty is passed through without assignment (e.g. a failed parse upstream leaves '').

Common situations: Optional form fields or CLI args that default to empty string; string slicing that produces '' (e.g. s[2:] on a 2-char string); stripping a prefix before checking emptiness.

Related errors


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