Python – 检查浮点字符串

Python – 检查浮点字符串

这些字符串由字符组成,要将其转换为浮点数据类型,Python为我们提供了使用内置函数的各种方法。借助这些内置函数,我们可以轻松检查浮点字符串。在本文中,用户将学习如何检查字符串是否包含浮点值。为了实现这一点,演示了三种验证浮点字符串的方法。还使用了isdigit()方法来检查给定字符串是否只包含数字。然后使用count()和replace()来检查字符串是否包含小数值和其他数字的字符。

方法

方法1 – 使用try-except块

方法2 – 使用正则表达式

方法3 – 使用isdigit()方法

方法1:使用try-except块检查浮点字符串的Python程序

定义了一个带有字符串变量的函数,并使用try-except方法,打印语句返回值为“True”或“False”。

算法

  • 步骤1 - 定义一个带有“str”变量的参数的函数。

  • 步骤2 - 如果参数包含浮点值,则返回“True”,否则返回“false”。

  • 步骤3 - 在函数内部,使用float()方法将给定的字符串转换为浮点类型,成功后返回true,否则返回false。

  • 步骤4 - 将输入“str1”初始化为浮点字符串值。

  • 步骤5 - 根据str的值打印语句。

示例

#function is defined with one parameter as str
def str_float(str1):
   try:
      int(str1)
      #returns false if it is an integer value
      return False
   except ValueError:
      #returns error if it is not a float value
      pass
   try:
      #float method is used to convert the given string to float
      float(str1)
      #returns True if it is a float value
      return True
   except ValueError:
      #returns false if it is not a float value
      return False
#str is initialized with a float value
str1 = "38.90"
if str_float(str1):
   #if true this statement will be returned
   print("Yes! float string")
else:
   #if false this statement will be returned
   print("No! float string ")

输出

Yes! float string

方法2:使用正则表达式检查浮点字符串的 Python 程序

该函数定义了一个正则表达式模式,其中包括输入字符串中可能包含的所有可能性,如符号(+ 或 -)、整数(从 0 到多个)、指数和小数值。

算法

  • 步骤 1 ‚àí 导入‚Äúre‚Äù库以使用正则表达式方法。

  • 步骤 2 ‚àí 定义一个带有一个参数的函数,该参数存储‚Äústr‚Äù变量。

  • 步骤 3 ‚àí 当字符串为浮点数据类型时,它返回‚ÄúTrue‚Äù,否则返回‚Äúfalse‚Äù。

  • 步骤 4 ‚àí 在函数内部,使用re.match()方法来查找输入字符串与给定正则表达式之间的匹配。

  • 步骤 5 ‚àí 初始化包含浮点值的变量stg。

  • 步骤 6 ‚àí 根据str的值,打印语句。

示例

#importing the re module
import re

#function is defined with one parameter as stg
def str_float(stg):
   regular_exp = re.compile("r'^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$")
   if regular_exp.match(stg):
      if "." in string:
         return True
      else:
         return False
   else:
       return False#str is initialized with a float value
stg = "38.90"
if str_float(stg):
   #if true this statement will be returned
   print("Yes! float string")
else:
   #if false this statement will be returned
   print("No! float string")

输出

Yes! float string

方法3:使用isdigit()方法检查浮点数字符串的Python程序

算法

  • 步骤1 - 定义一个带有“str”变量的参数的函数。

  • 步骤2 - 使用布尔函数来检查字符串是否符合isdigit()、count()和replace()方法的条件,并返回相应的值。

  • 步骤3 - 当字符串为浮点数类型时,在前两个条件下返回“True”,否则返回“false”。

  • 步骤4 - 指定输入“str1”的浮点数字符串值。

  • 步骤5 - 最后打印该语句。

示例

# function is defined with one parameter as str
def str_float(str1):
   if str1.isdigit():
      return False
   else:
      try:
         float(str1)
         return True
      except ValueError:
         return False# str is initialized with a float value
str1 = "38.90"
if str_float(str1):
   # if true this statement will be returned
   print("Yes! float string")
else:
   # if false this statement will be returned
   print("No! float string")

输出

Yes! float string

结论

Python程序专门用于最小化手动工作和错误。要简单检查给定的字符串输入是否为浮点数,有许多方法。上面讨论了一些方法,包括使用try和except方法以及使用isdigit()函数来检查浮点字符串,当输入为整数时,它也会返回false。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程