{"record":{"id":"5646fc16ba79f196","repo":"TheAlgorithms/Python","slug":"empty-string-was-passed-to-the-function-5646fc","errorCode":null,"errorMessage":"Empty string was passed to the function","messagePattern":"Empty string was passed to the function","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/hexadecimal_to_decimal.py","lineNumber":34,"sourceCode":"    65535\n    >>> hex_to_decimal(\"-Ff\")\n    -255\n    >>> hex_to_decimal(\"F-f\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Non-hexadecimal value was passed to the function\n    >>> hex_to_decimal(\"\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Empty string was passed to the function\n    >>> hex_to_decimal(\"12m\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Non-hexadecimal value was passed to the function\n    \"\"\"\n    hex_string = hex_string.strip().lower()\n    if not hex_string:\n        raise ValueError(\"Empty string was passed to the function\")\n    is_negative = hex_string[0] == \"-\"\n    if is_negative:\n        hex_string = hex_string[1:]\n    if not all(char in hex_table for char in hex_string):\n        raise ValueError(\"Non-hexadecimal value was passed to the function\")\n    decimal_number = 0\n    for char in hex_string:\n        decimal_number = 16 * decimal_number + hex_table[char]\n    return -decimal_number if is_negative else decimal_number\n\n\nif __name__ == \"__main__\":\n    from doctest import testmod\n\n    testmod()\n","sourceCodeStart":16,"sourceCodeEnd":50,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/hexadecimal_to_decimal.py#L16-L50","documentation":"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.","triggerScenarios":"hex_to_decimal(''), hex_to_decimal('\\t\\n'), or an empty field from a form/parser flowing into the function.","commonSituations":"CSV rows with missing columns; JSON payloads where a hex field is present but empty; test harnesses iterating over possibly-empty tokens.","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"],"exampleFix":"# before\nfor cell in row:\n    out.append(hex_to_decimal(cell))  # empty cell -> ValueError\n\n# after\nfor cell in row:\n    if cell.strip():\n        out.append(hex_to_decimal(cell))","handlingStrategy":"validation","validationCode":"if not isinstance(hex_string, str) or not hex_string.strip():\n    raise ValueError('hex string is required')","typeGuard":"def is_nonempty_hex_input(v: object) -> TypeGuard[str]:\n    return isinstance(v, str) and bool(v.strip())","tryCatchPattern":null,"preventionTips":["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"],"tags":["python","value-error","hex","decimal","empty-input"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}