检查字符串是否为实数 [英] Check if string is a real number
本文介绍了检查字符串是否为实数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种快速的方法来查找字符串是否为实数,而不是一次读取一个字符并对每个字符执行 isdigit()
?我希望能够测试浮点数,例如 0.03001
.
Is there a quick way to find if a string is a real number, short of reading it a character at a time and doing isdigit()
on each character? I want to be able to test floating point numbers, for example 0.03001
.
推荐答案
如果你的意思是浮点数作为实数,这应该可行:
If you mean an float as a real number this should work:
def isfloat(str):
try:
float(str)
except ValueError:
return False
return True
请注意,这仍会在内部循环您的字符串,但这是不可避免的.
Note that this will internally still loop your string, but this is inevitable.
这篇关于检查字符串是否为实数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文