简体   繁体   English

Python字符串如何成为数字?

[英]How can a Python string be a digit?

I was under the impression that numbers in double-quotation marks become strings.我的印象是双引号中的数字会变成字符串。 Why did I get the following result?为什么我得到以下结果?

"2001".isdigit()
True

.isdigit() is a method for the str object if this string only contains digits.如果此字符串仅包含数字,则.isdigit()str对象的方法。

From the Python documentation , .isdigit() :Python 文档.isdigit()

Return true if all characters in the string are digits and there is at least one character, false otherwise.如果字符串中的所有字符都是数字并且至少有一个字符,则返回 true,否则返回 false。 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.这涵盖了不能用于​​形成以 10 为基数的数字的数字,例如 Kharosthi 数字。 Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.正式地,数字是具有属性值 Numeric_Type=Digit 或 Numeric_Type=Decimal 的字符。

It says .isdigit() not .isint() which makes sense since int and string are Python types, but nobody says a string can't be digits.它说.isdigit()而不是.isint() ,这是有道理的,因为intstring是 Python 类型,但没有人说字符串不能是数字。

In fact, strings can contain letters, alphanumeric, digits, and a bunch of other things.事实上,字符串可以包含字母、字母数字、数字和一堆其他东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 中获取 5 位十六进制字符串? - How can I get the string of 5 digit hexadecimal in Python? 如何在python中使用正则表达式获取字符串(数字和字符的混合)到最后一位? - How can I get a string(Mix of digit and char) up to last digit using regex in python? python如何按digit = string排序字符串列表 - python How to sort list of string by digit=string 如何在 Python 中生成 4 位字符串和 4 位数字的随机码? - How to Generate random code with 4 digit string and 4 digit number in Python? 如何在python中获取数字或字符串的第二个数字? - How to get the second digit of a number or string in python? 如何在 python 的字符串数组的开头添加数字? - How to add digit in the beginning of string array in python? Python:如何通过其索引引用字符串中的数字? - Python: How to refer to a digit in a string by its index? 如何检查 python 中字符串列表中的数字? - How to check for digit in list of string in python? 如何在python中使用多位通配符来使用各种模式解析字符串? - How can you employ multi-digit wildcards in python to parse a string using varying patterns? 如何将字符串中的数字乘以 python 中字符串中的 position - How to multiple a digit in a string by its position in the string in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM