0

Hi I think I see a lot of people having knowledge in depth in field of computer science.

My question is in python3, is there a way to check if an input statement or a string value is a float?

I am expecting something like

str.isfloat()

Thank you for taking a look for this poor student :)

user7693152
  • 9
  • 1
  • 5

1 Answers1

0

Pass it to the float constructor and catch the exception.

>>> float('1')
1.0
>>> float('2.0')
2.0
>>> float('three')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 'three'
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358