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

Python isdigit() method

Python isdigit() method

Python Strings Python Strings


Description

The Python isdigit() method checks whether a string consists only of digits; it is only valid for 0 and positive numbers.

Syntax

Syntax of the isdigit() method:



str.isdigit()

Parameters

Return Value

Returns True if the string contains only digits, otherwise False.

Example

The following example demonstrates the isdigit() method:

Example

#!/usr/bin/python

-- coding: UTF-8 --

str

=

"

123456

"

The string contains only digits

print

(

str

.

isdigit

(

)

)

str

=

"

this is string example....wow!!!

"

print

(

str

.

isdigit

(

)

)

str

=

"

0

"

print

(

str

.

isdigit

(

)

)

str

=

"

-1

"

print

(

str

.

isdigit

(

)

)

The output of the above example is:



True

False

True

False


Python Strings Python Strings