How do you check in python whether a string contains only numbers?
Use string isdigit function:
>>> s = '12345'
>>> s.isdigit()
True
>>> s = '1abc'
>>> s.isdigit()
False
You'll want to use the isdigit
method on your str
object:
if len(isbn) == 10 and isbn.isdigit():
From the isdigit
documentation:
str.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.
Use str.isdigit
:
>>> "12345".isdigit()
True
>>> "12345a".isdigit()
False
>>>