Python isdigit() Method

Python 字符串Python String


Description

Python isdigit() method checks whether the string consists only of digits. It only works for 0 and positive numbers.

Syntax

isdigit() method syntax:

str.isdigit()

Parameters

  • None.

Return Value

Returns True if the string contains only digits, otherwise returns 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 字符串Python String

Other Extensions