TheAlgorithms/Python · error · TypeError
'str' object cannot be interpreted as an integer
Error message
'str' object cannot be interpreted as an integer
What it means
Raised by decimal_to_binary_iterative(num) in conversions/decimal_to_binary.py:33 when `num` is a str. The function does no string parsing — not even numeric strings like '0xfffff' or '42' — because the shift/modulo loop requires an int. Callers must parse strings themselves before calling.
Source
Thrown at conversions/decimal_to_binary.py:33
>>> # negatives work too
>>> decimal_to_binary_iterative(-2)
'-0b10'
>>> # other floats will error
>>> decimal_to_binary_iterative(16.16) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: 'float' object cannot be interpreted as an integer
>>> # strings will error as well
>>> decimal_to_binary_iterative('0xfffff') # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: 'str' object cannot be interpreted as an integer
"""
if isinstance(num, float):
raise TypeError("'float' object cannot be interpreted as an integer")
if isinstance(num, str):
raise TypeError("'str' object cannot be interpreted as an integer")
if num == 0:
return "0b0"
negative = False
if num < 0:
negative = True
num = -num
binary: list[int] = []
while num > 0:
binary.insert(0, num % 2)
num >>= 1
if negative:
return "-0b" + "".join(str(e) for e in binary)
View on GitHub (pinned to f5988cc097)
Solutions
- Parse the string yourself first: decimal_to_binary_iterative(int('42')) for decimal, int('0xfffff', 0) or int(s, 16) for hex
- For raw user input, wrap parsing in try/except ValueError to surface a friendly message
- Note decimal_to_binary_recursive (same module) does accept strings — switch to it if string input is the norm
Example fix
# before
decimal_to_binary_iterative('0xfffff') # TypeError
# after
decimal_to_binary_iterative(int('0xfffff', 16)) # '0b11111111111111111111' Defensive patterns
Strategy: type-guard
Validate before calling
if isinstance(num, str):
num = int(num) # decimal strings
# or int(num, 16) / int(num, 0) for prefixed strings Type guard
def is_int_not_str(v: object) -> TypeGuard[int]:
return isinstance(v, int) Prevention
- Parse CLI/file input to int before any conversion call
- For string inputs generally, prefer decimal_to_binary_recursive from the same module
- Annotate parameters int and run mypy to catch string flows at build time
When it happens
Trigger: decimal_to_binary_iterative('0xfffff'), decimal_to_binary_iterative('42'), or forwarding raw stdin/argparse/UI input into the function.
Common situations: Feeding CLI arguments (always strings) or file/network payloads directly to the converter; assuming the function is lenient like int() itself.
Related errors
- 'float' object cannot be interpreted as an integer
- operation can not be conducted on an object of type {type(nu
- Input value must be an 'int' type
- plaintext must be a string
- key must be a string
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/f337d2632939304a.
Report an issue: GitHub.