-6

I was under the impression that numbers in double-quotation marks become strings. Why did I get the following result?

"2001".isdigit()
True
Community
  • 1
  • 1
Josh Ossai
  • 17
  • 1
  • 2
    Well yeah, but `str.isdigit` is a _string method_ that checks if a _string_ is comprised entirely of digits. – miradulo May 18 '18 at 01:01
  • 1
    It's even better: 2001 is not a digit, it's a natural number. But "2001" is a digit that represents the natural number 2001. – Andrey Tyukin May 18 '18 at 01:18

2 Answers2

3

.isdigit() is a method for the str object if this string only contains digits.

From 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.

Shadow
  • 8,749
  • 4
  • 47
  • 57
2

It says .isdigit() not .isint() which makes sense since int and string are Python types, but nobody says a string can't be digits.

In fact, strings can contain letters, alphanumeric, digits, and a bunch of other things.

Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
Zilong Li
  • 889
  • 10
  • 23