ValueError: invalid literal for int() with base 10 (tested)
ValueError: invalid literal for int() with base 10: '...' means int() was given a string that isn't a plain whole number. We measured exactly which strings fail on Python 3.13.5 and how to convert each kind.
1. What int() accepts and rejects
for s in ["42", " 42\n", "4.5", "", "1,000", "42abc", "0x1f", "١٢", "1_000"]:
try:
print(repr(s), "->", int(s))
except ValueError as e:
print(repr(s), "->", e)
Output:
'42' -> 42
' 42\n' -> 42
'4.5' -> invalid literal for int() with base 10: '4.5'
'' -> invalid literal for int() with base 10: ''
'1,000' -> invalid literal for int() with base 10: '1,000'
'42abc' -> invalid literal for int() with base 10: '42abc'
'0x1f' -> invalid literal for int() with base 10: '0x1f'
'١٢' -> 12
'1_000' -> 1000
Measured on Python 3.13.5: int() strips surrounding whitespace (including the \n from readline()), and accepts underscores and non-ASCII digits like Arabic-Indic ١٢. It rejects decimals, the empty string (an empty line or form field, the most common cause), thousands separators, trailing units and 0x prefixes. The message shows the exact string in quotes: look closely at it.
2. Fixes for decimals, separators and other bases
print(int(float("4.5")), int(float("4.9")), round(float("4.5")), round(float("5.5")))
print(int("1,000".replace(",", "")))
print(int("0x1f", 16), int("0x1f", 0), int("ff", 16), int("0b101", 0))
try:
int("010", 0)
except ValueError as e:
print('int("010", 0):', e)
Output:
4 4 4 6
1000
31 31 255 5
int("010", 0): invalid literal for int() with base 0: '010'
Measured: int(float(s)) truncates toward zero (4.9 → 4); round() uses banker's rounding (4.5 → 4, 5.5 → 6). Strip separators before converting. Pass a base for hex/binary; base 0 reads the prefix but rejects leading zeros like "010".
3. Safe conversion of user input
Piped " 12" into input():
def to_int(s, default=None):
try:
return int(s)
except (ValueError, TypeError):
return default
for s in ["7", "", None, "seven"]:
print(repr(s), "->", to_int(s))
x = input("number? ") # input() always returns a str
print(x.strip().isdigit(), "²".isdigit())
Output:
'7' -> 7
'' -> None
None -> None
'seven' -> None
number? True True
For values that may be missing or junk, wrap int() in try/except (ValueError, TypeError) and return a default. Avoid isdigit() as a pre-check: we measured "²".isdigit() returning True, yet int("²") fails, and isdigit() rejects "-5".
Frequently asked questions
What does ValueError: invalid literal for int() with base 10 mean?
int() got a string that isn't a whole number in base 10. We measured '4.5', '', '1,000', '42abc' and '0x1f' all failing; the quoted value in the message is the exact input.
Why does int('') fail?
An empty string isn't a number; it usually comes from an empty line, blank CSV cell or form field. We measured the message invalid literal for int() with base 10: ''.
How do I convert '4.5' to int in Python?
int(float('4.5')) gives 4 (truncates). round(float(s)) rounds, but half to even: we measured 4.5 → 4 and 5.5 → 6.
Is isdigit() a safe check before int()?
No. We measured '²'.isdigit() returning True although int('²') raises, and isdigit() rejects negative numbers. Use try/except ValueError instead.
Written with AI assistance. Measured on 2026-10-09 with Python 3.13.5 on Linux: int() on whitespace, decimals, empty strings, separators, suffixes, hex, Arabic-Indic digits and underscores; int(float()), round(), explicit bases and base 0; a safe to_int helper and isdigit(). Every block was run verbatim and its output pasted from that run. PlainHeaders is our extension; everything else here is built-in Python.