TheAlgorithms/Python · error · ValueError

Empty string was passed to the function

Error message

Empty string was passed to the function

What it means

bin_to_hexadecimal raises this ValueError when the input string is empty after str() conversion and .strip(). The empty check runs before sign handling and digit validation, so blank input is reported specifically as 'empty' rather than 'non-binary', which makes caller-side error messages actionable.

Source

Thrown at conversions/binary_to_hexadecimal.py:45

    >>> bin_to_hexadecimal(' 1010   ')
    '0x0a'
    >>> bin_to_hexadecimal('-11101')
    '-0x1d'
    >>> bin_to_hexadecimal('a')
    Traceback (most recent call last):
        ...
    ValueError: Non-binary value was passed to the function
    >>> bin_to_hexadecimal('')
    Traceback (most recent call last):
        ...
    ValueError: Empty string was passed to the function
    """
    # Sanitising parameter
    binary_str = str(binary_str).strip()

    # Exceptions
    if not binary_str:
        raise ValueError("Empty string was passed to the function")
    is_negative = binary_str[0] == "-"
    binary_str = binary_str[1:] if is_negative else binary_str
    if not all(char in "01" for char in binary_str):
        raise ValueError("Non-binary value was passed to the function")

    binary_str = (
        "0" * (4 * (divmod(len(binary_str), 4)[0] + 1) - len(binary_str)) + binary_str
    )

    hexadecimal = []
    for x in range(0, len(binary_str), 4):
        hexadecimal.append(BITS_TO_HEX[binary_str[x : x + 4]])
    hexadecimal_str = "0x" + "".join(hexadecimal)

    return "-" + hexadecimal_str if is_negative else hexadecimal_str


if __name__ == "__main__":

View on GitHub (pinned to f5988cc097)

Solutions

  1. Guard before calling: if not binary_str.strip(): handle missing value
  2. Skip empty tokens when mapping over collections: filter(None, map(str.strip, tokens))
  3. Treat empty as '0' if that is the intended domain behavior: binary_str or '0'

Example fix

# before
values = '101,,111'.split(',')
[bin_to_hexadecimal(v) for v in values]
# ValueError: Empty string was passed to the function

# after
[bin_to_hexadecimal(v) for v in values if v.strip()]
Defensive patterns

Strategy: validation

Validate before calling

binary_str = str(binary_str or '').strip()
if not binary_str:
    binary_str = '0'
bin_to_hexadecimal(binary_str)

Type guard

def is_nonempty_str(v) -> bool:
    return isinstance(v, str) and v.strip() != ''

Try / catch

try:
    bin_to_hexadecimal(s)
except ValueError as e:
    if 'Empty string' in str(e):
        return '0x0'
    raise

Prevention

When it happens

Trigger: bin_to_hexadecimal(''), bin_to_hexadecimal(' '), bin_to_hexadecimal('\t') — any whitespace-only or empty value.

Common situations: Form fields submitted empty; split() producing empty tokens when the delimiter repeats ('101,,111'.split(',')); CSV columns with missing values passed through unchanged.

Related errors


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