Python isdigit() method
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
- none.
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
(
str
.
isdigit
(
)
)
str
=
"
this is string example....wow!!!
"
(
str
.
isdigit
(
)
)
str
=
"
0
"
(
str
.
isdigit
(
)
)
str
=
"
-1
"
(
str
.
isdigit
(
)
)
The output of the above example is:
True
False
True
False
Python Strings