Python: Exceeds the limit for integer string conversion

Bug or feature?

Fullstack CTO
2 min readOct 24, 2022
>>> # Python 3.10.6
>>> int(“2” * 5432)
>>> 222222222222222222222222222222222222222222222222222222222222222…
>>> # Python 3.10.8 and Python 3.11.0
>>> int("2" * 5432)
>>> Traceback (most recent call last):

ValueError: Exceeds the limit (4300) for integer string conversion: value has 5432 digits; use sys.set_int_max_str_digits() to increase the limit.

💡 Explanation

This call to int() works fine in Python 3.10.6 and raises a ValueError in Python 3.10.8. Note that Python can still work with large integers. The error is only raised when converting between integers and strings.

Fortunately, you can increase the limit for the allowed number of digits when you expect an operation to exceed it. To do this, you can use one of the following:

  • The -X int_max_str_digits command-line flag
  • The set_int_max_str_digits() function from the sys module
  • The PYTHONINTMAXSTRDIGITS environment variable

Check the documentation for more details on changing the default limit if you expect your code to exceed this value.

In Python 3.11.0rc2 this behavior not fixed:

--

--