How do you check in python whether a string contains only numbers?
How do you check whether a string contains only numbers?
I've given it a go here. I'd like to see the simplest way to accomplish this.
import string def main(): isbn = input("Enter your 10 digit ISBN number: ") if len(isbn) == 10 and string.digits == True: print ("Works") else: print("Error, 10 digit number was not inputted and/or letters were inputted.") main() if __name__ == "__main__": main() input("Press enter to exit: ")
You'll want to use the
isdigit method on your
if len(isbn) == 10 and isbn.isdigit():
> str.isdigit() >
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.