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 hex_to_decimal(hex_string) in conversions/hexadecimal_to_decimal.py:34 when the input strips to an empty string. The parser needs at least one hex digit; empty/whitespace-only input has nothing to evaluate, so it raises ValueError('Empty string was passed to the function') before the digit loop.
Source
Thrown at conversions/hexadecimal_to_decimal.py:34
65535
>>> hex_to_decimal("-Ff")
-255
>>> hex_to_decimal("F-f")
Traceback (most recent call last):
...
ValueError: Non-hexadecimal value was passed to the function
>>> hex_to_decimal("")
Traceback (most recent call last):
...
ValueError: Empty string was passed to the function
>>> hex_to_decimal("12m")
Traceback (most recent call last):
...
ValueError: Non-hexadecimal value was passed to the function
"""
hex_string = hex_string.strip().lower()
if not hex_string:
raise ValueError("Empty string was passed to the function")
is_negative = hex_string[0] == "-"
if is_negative:
hex_string = hex_string[1:]
if not all(char in hex_table for char in hex_string):
raise ValueError("Non-hexadecimal value was passed to the function")
decimal_number = 0
for char in hex_string:
decimal_number = 16 * decimal_number + hex_table[char]
return -decimal_number if is_negative else decimal_number
if __name__ == "__main__":
from doctest import testmod
testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Filter or reject blank tokens upstream: if not hex_string.strip(): continue / raise
- Treat empty as zero explicitly if that is your domain's semantics: hex_to_decimal(s or '0')
- Add presence checks when parsing files so empty columns never propagate
Example fix
# before
for cell in row:
out.append(hex_to_decimal(cell)) # empty cell -> ValueError
# after
for cell in row:
if cell.strip():
out.append(hex_to_decimal(cell)) Defensive patterns
Strategy: validation
Validate before calling
if not isinstance(hex_string, str) or not hex_string.strip():
raise ValueError('hex string is required') Type guard
def is_nonempty_hex_input(v: object) -> TypeGuard[str]:
return isinstance(v, str) and bool(v.strip()) Prevention
- Skip blank cells/fields during parsing instead of passing them on
- Use s or '0' when zero is the intended meaning of empty
- Validate presence of required string columns at load time
When it happens
Trigger: hex_to_decimal(''), hex_to_decimal('\t\n'), or an empty field from a form/parser flowing into the function.
Common situations: CSV rows with missing columns; JSON payloads where a hex field is present but empty; test harnesses iterating over possibly-empty tokens.
Related errors
- No value was passed to the function
- Non-hexadecimal value was passed to the function
- Empty string was passed to the function
- No input value was provided
- Invalid value was passed to the function
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/5646fc16ba79f196.
Report an issue: GitHub.