{"record":{"id":"a77e3f4b7a42e339","repo":"TheAlgorithms/Python","slug":"empty-string-was-passed-to-the-function-a77e3f","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/octal_to_hexadecimal.py","lineNumber":29,"sourceCode":"    Traceback (most recent call last):\n        ...\n    TypeError: Expected a string as input\n    >>> octal_to_hex(\"Av\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Not a Valid Octal Number\n    >>> octal_to_hex(\"\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Empty string was passed to the function\n    \"\"\"\n\n    if not isinstance(octal, str):\n        raise TypeError(\"Expected a string as input\")\n    if octal.startswith(\"0o\"):\n        octal = octal[2:]\n    if octal == \"\":\n        raise ValueError(\"Empty string was passed to the function\")\n    if any(char not in \"01234567\" for char in octal):\n        raise ValueError(\"Not a Valid Octal Number\")\n\n    decimal = 0\n    for char in octal:\n        decimal <<= 3\n        decimal |= int(char)\n\n    hex_char = \"0123456789ABCDEF\"\n\n    revhex = \"\"\n    while decimal:\n        revhex += hex_char[decimal & 15]\n        decimal >>= 4\n\n    return \"0x\" + revhex[::-1]\n\n","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/octal_to_hexadecimal.py#L11-L47","documentation":"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.","triggerScenarios":"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 '').","commonSituations":"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.","solutions":["Check for empty/whitespace input before calling: if not octal or octal.strip('o0') == '' ... reject.","Treat missing values as an error or default (e.g. '0') at the input boundary, not inside the conversion call.","Validate required fields in the form/API layer before invoking the converter."],"exampleFix":"# before\noctal_to_hex(\"\")  # ValueError: Empty string was passed to the function\n\n# after\nif not octal or octal.removeprefix('0o') == '':\n    raise ValueError('octal input is required')\noctal_to_hex(octal)","handlingStrategy":"validation","validationCode":"if not isinstance(octal, str) or not octal.removeprefix('0o'):\n    raise ValueError('octal value is required')\noctal_to_hex(octal)","typeGuard":"def has_octal_payload(octal: object) -> bool:\n    return isinstance(octal, str) and len(octal.removeprefix('0o')) > 0","tryCatchPattern":"try:\n    octal_to_hex(octal)\nexcept ValueError as e:\n    if 'Empty string' in str(e):\n        octal = '0'  # or prompt the user again\n    else:\n        raise","preventionTips":["Treat empty input as missing data at the form/parse boundary","Remember the '0o' prefix alone also strips to empty","Default required color/base fields to '0' rather than ''"],"tags":["python","validation","empty-input","conversion"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}