`base` in `str()`

I propose to add an optional keyword-only argument base to str() (symmetrically to base in int()).

Just like n.toString(base) in Kotlin, to!string(n, base) in D, String(n, radix: base) in Swift, or String(n, radix' base) in 11l.

This can be useful for some tasks on Rosetta Code (for example in Esthetic numbers to_digits(num, base) can be replaced with str(num, base = base)).

Do you find this feature useful?
  • Yes
  • Maybe
  • No

0 voters

It seems like an esoteric parameter that doesn’t have any value outside of one specific use: number stringification. I think it would confuse readers of str’s type signature (shhh, yes I know about the decoding parameters).

Perhaps instead a method of int?


I agree with @EpicWink about this being the wrong place (wrong API). In builtins is where I would expect it, alongside hex(), or in int or the math library.

It seems a curious omission that there is no built-in conversion to arbitrary base that I can find. PyNumber_ToBase (_PyLong_Format really) goes to great lengths to optimise the few bases it supports but there’s not even a slow path if you want 6*9 base 13.

1 Like

This may be better as part of the format specifier for integers, so e.g. f"{10:b5}" would format 10 in base 5.

In fact, this approach has been suggested before.