If you want to check whether a Python string is an integer, you can try
casting to an int with int()
and catching the ValueError
if it’s not an
integer:
|
|
To check for nonnegative integers, you can use the str.is_digit()
method. It
will “return true if all characters in the string are digits and there is at
least one character, false otherwise:
|
|
Thanks to Jeremy Kahn for reminding
me that isdigit
only
detects positive integers.