Skip to main content
 首页 » 编程设计

python之为什么 int() 的基值限制在 36 到 2 之间

2025年05月04日9开发

我发现当我在 python 中运行以下表达式时

int(33, base=100) 

我收到以下错误:

ValueError: int() base must be >= 2 and <= 36, or 0 

我找不到关于此限制的任何解释; another Stack Overflow question about the same error message没有提到为什么 base 参数被限制在这个范围内。

请您参考如下方法:

int 的 base 参数用于解析字符串,而不是在将 int 传递给 int 时使用:

>>> int('ff', 16) 
255 

对于代表 10、11、12、...、35 的数字使用 a、b、c、...、z 是一个公认的约定,但是对于数字 36 使用什么符号没有约定基数 37。