# Expose possibility to format ints in all supported bases?

**URL:** <https://discuss.python.org/t/expose-possibility-to-format-ints-in-all-supported-bases/108783>\
**Category:** Ideas\
**Created:** [August 28, 2026, 4:17am UTC](https://discuss.python.org/t/expose-possibility-to-format-ints-in-all-supported-bases/108783 "2026-08-28T04:17:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![skirpichev](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/skirpichev/32/10996_2.png) [@skirpichev](https://discuss.python.org/u/skirpichev)\
**Post date:** [August 28, 2026, 4:17am UTC](https://discuss.python.org/t/expose-possibility-to-format-ints-in-all-supported-bases/108783/1 "2026-08-28T04:17:02Z")

</div>

Despite int constructor accepts strings in any base in `range(2, 37)`, currently we can format numbers only in decimal, binary, octal or hexadecimal. (str, repr, bin, oct, hex and format builtins, respectively.)

Occasionally, for debugging and/or interoperation it’s useful to print numbers in other bases. Unfortunately, we don’t have a method for this: users have to reinvent the wheel here.

Such support was requested [before](https://discuss.python.org/t/arbitrary-base-for-integer-literals-and-integers-in-formatted-strings/15275) in new-style string formatting. Though, this introduce complications in the string formatting mini-language for rather niche case. How about simpler API, just a new method for int’s?

All (direct or indirect) Python interfaces to the GNU GMP have such methods. The `gmpy2` and `python-gmp` packages have [`digits()`](https://python-gmp.readthedocs.io/en/latest/#gmp.mpz.digits) method. The `python-flint` has [`str()`](https://python-flint.readthedocs.io/en/latest/fmpz.html#flint.fmpz.str) method, similar to Sagemath’s [`str()`](https://doc.sagemath.org/html/en/reference/rings_standard/sage/rings/integer.html#sage.rings.integer.Integer.str). An example:

```pycon
>>> import flint
>>> n = flint.fmpz.fac_ui(50)
>>> n.str(base=32)
'ifenib1tkjpm0lhtt7ihkcqgjqpeegt5hs000000000'

```

I propose this method for integer objects:

```py
def digits(self, /, base=10):
    """
    Return string representing self in the given base.

    Values for base can range between 2 to 36.
    """
    ...

```

On the C-API side we could relax requirements on the base argument for [`PyNumber_ToBase()`](https://docs.python.org/3.14/c-api/number.html#c.PyNumber_ToBase) functions (currently accepting 2, 8, 10 and 16).

Note, that all stuff is already implemented (and probably much better than a homegrown code snippet from a random “inventor”) in the CPython (e.g. `_PyLong_Format()` private function) and in alternative interpreters I know.
