Python isdigit

Python isdigit 字符串函数用于检查字符是否为数字。在本节中,我们将讨论如何使用示例编写或使用此字符串 isdigit 函数,其语法如下:

String_Value.isdigit()

它返回一个布尔值作为输出。

  • 如果 String_Value 是一个有效的数字,则返回 TRUE
  • 如果 String_Value 不是,则返回 FALSE。

使用 isdigit 函数检查数字的 Python 程序

以下示例集将帮助您理解 isdigit 函数以及如何将其用于浮点数、正数和负数。

首先,我们声明字符串变量 Str1 并使用以下语句为其分配相应的值(整数值)。接下来,我们使用它来检查字符串是否为数字。

从下面的代码可以看出,123456789 是一个有效的数字。因此,字符串方法返回 TRUE。以下Python语句检查空格是否为有效数字。

接下来,我们将字符串 isdigit 函数用于以下一组单词。

Str1 = '123456789';
print('First Output of a method is = ', Str1.isdigit())

Str2 = '     ';
print('Output of a method For Empty Space is = ', Str2.isdigit())

# Performing function directly
Str3 = 'Find Tutorial at Tutorial Gateway'.isdigit()
print('Second Output  = ', Str3)

# Performing on both Digits and Alphabets
Str5 = '123456789a'.isdigit()
print('Third Output  = ', Str5)

# Performing on Float values
Str6 = '1234.67'.isdigit()
print('Fourth Output  = ', Str6)

# Performing on Negative Values  
Str7 = '-1234'.isdigit()
print('Fifth Output  = ', Str7)
IsDigit function to check string is a digit