Python 字符串如何成为数字?

How can a Python string be a digit?

我的印象是双引号中的数字变成了字符串。为什么我会得到以下结果?

"2001".isdigit()
True
如果此字符串仅包含数字,

.isdigit()str 对象的方法。

来自 the Python documentation.isdigit()

Return true if all characters in the string are digits and there is at least one character, false otherwise. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers. Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.

它说 .isdigit() 而不是 .isint() 这是有道理的,因为 intstring 是 Python 类型,但没有人说字符串不能数字。

事实上,字符串可以包含字母、字母数字、数字和许多其他内容。