TheAlgorithms/Python · error · TypeError
Expected a string as input
Error message
Expected a string as input
What it means
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.
Source
Thrown at conversions/octal_to_hexadecimal.py:25
'0x40'
>>> octal_to_hex("235")
'0x9D'
>>> octal_to_hex(17)
Traceback (most recent call last):
...
TypeError: Expected a string as input
>>> octal_to_hex("Av")
Traceback (most recent call last):
...
ValueError: Not a Valid Octal Number
>>> octal_to_hex("")
Traceback (most recent call last):
...
ValueError: Empty string was passed to the function
"""
if not isinstance(octal, str):
raise TypeError("Expected a string as input")
if octal.startswith("0o"):
octal = octal[2:]
if octal == "":
raise ValueError("Empty string was passed to the function")
if any(char not in "01234567" for char in octal):
raise ValueError("Not a Valid Octal Number")
decimal = 0
for char in octal:
decimal <<= 3
decimal |= int(char)
hex_char = "0123456789ABCDEF"
revhex = ""
while decimal:
revhex += hex_char[decimal & 15]
decimal >>= 4View on GitHub (pinned to f5988cc097)
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.
Example fix
# before octal_to_hex(777) # TypeError: Expected a string as input # after octal_to_hex(str(777)) # '1FF'
Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(octal, str):
octal = str(octal) # or reject: raise TypeError from your own layer
octal_to_hex(octal) Type guard
def is_octal_str(octal: object) -> bool:
return isinstance(octal, str) and all(c in '01234567' for c in octal.removeprefix('0o')) Try / catch
try:
octal_to_hex(value)
except TypeError as e:
if 'Expected a string' in str(e):
value = str(value)
result = octal_to_hex(value)
else:
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Non-octal value was passed to the function
- Not a Valid Octal Number
- Expected int, found {type(r_input), type(g_input), type(b_in
- operation can not be conducted on an object of type {type(nu
- Input value must be an 'int' type
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/742bcec46e1496c1.
Report an issue: GitHub.