Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Check Whether a String Consists of Digits

Python Check Whether a String Consists of Digits

Document Object Reference Manual Python3 Examples

We can use Python's str.isdigit() method to determine whether a string consists entirely of digits. This method returns a boolean: it returns True; otherwise, returns False。

Example

def

is_all_digits

(

s

)

:

   

return

s.

isdigit

(

)

Testing

test_string1

=

"12345"

test_string2

=

"123a45"

print

(

is_all_digits

(

test_string1

)

)

 

Output True

print

(

is_all_digits

(

test_string2

)

)

 

Output False

Code analysis:

Output result:

True
False

Document Object Reference Manual Python3 Examples