{"record":{"id":"742bcec46e1496c1","repo":"TheAlgorithms/Python","slug":"expected-a-string-as-input","errorCode":null,"errorMessage":"Expected a string as input","messagePattern":"Expected a string as input","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"conversions/octal_to_hexadecimal.py","lineNumber":25,"sourceCode":"    '0x40'\n    >>> octal_to_hex(\"235\")\n    '0x9D'\n    >>> octal_to_hex(17)\n    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","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/octal_to_hexadecimal.py#L7-L43","documentation":"Raised by octal_to_hex() in conversions/octal_to_hexadecimal.py when the `octal` argument is not a str instance. The function's contract is string-only input; ints, floats, None, or bytes all hit the isinstance check and raise TypeError immediately.","triggerScenarios":"Calling octal_to_hex(777) with an int, octal_to_hex(None), octal_to_hex(b'777'), or passing a value that came from a JSON number field.","commonSituations":"Assuming the converter accepts integers like int(octal_string, 8) would; reading mixed-type data (API responses, CSV columns) where the field is sometimes numeric; passing a default None when a lookup fails.","solutions":["Convert to str first: octal_to_hex(str(value)) when value is an int.","Fix the upstream loader to keep the field as a string (e.g. dtype=str when reading CSV).","Add an isinstance(octal, str) check at the call site for untrusted data."],"exampleFix":"# before\noctal_to_hex(777)  # TypeError: Expected a string as input\n\n# after\noctal_to_hex(str(777))  # '1FF'","handlingStrategy":"type-guard","validationCode":"if not isinstance(octal, str):\n    octal = str(octal)  # or reject: raise TypeError from your own layer\noctal_to_hex(octal)","typeGuard":"def is_octal_str(octal: object) -> bool:\n    return isinstance(octal, str) and all(c in '01234567' for c in octal.removeprefix('0o'))","tryCatchPattern":"try:\n    octal_to_hex(value)\nexcept TypeError as e:\n    if 'Expected a string' in str(e):\n        value = str(value)\n        result = octal_to_hex(value)\n    else:\n        raise","preventionTips":["Keep octal values as strings end-to-end in your data pipeline","Read mixed CSV/JSON columns with dtype=str so numbers never reach this API","Guard with isinstance(x, str) when the source is untrusted"],"tags":["python","type-error","conversion","numeral-system"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}