Coder77 Coder77 - 9 months ago 266
Python Question

How do you check if a string contains ONLY numbers - python

How do you check if a string contains only numbers?
I've given it a go here, need it in the simplest way. Thanks.

import string

def main():
isbn = input("Enter you're 10 digit ISBN number: ")
if len(isbn) == 10 and string.digits == True:
print ("Works")
print("Error, 10 digit number was not inputted and/or letters were inputted.")

if __name__ == "__main__":
input("Press enter to exit: ")


You'll want to use the isdigit method on your str object:

if len(isbn) == 10 and isbn.isdigit():

From the isdigit documentation:


Return true if all characters in the string are digits and there is at least one character, false otherwise.

For 8-bit strings, this method is locale-dependent.