Testing if Python string variable holds number (int,float) or non-numeric str?

前端 未结 6 978
情书的邮戳
情书的邮戳 2020-12-19 11:23

If a Python string variable has had either an integer, floating point number or a non-numeric string placed in it, is there a way to easily test the \"type\" of that value?<

6条回答
  •  没有蜡笔的小新
    2020-12-19 11:42

    use .isdigit():

    In [14]: a = '145'
    
    In [15]: b = 'foo'
    
    In [16]: a.isdigit()
    Out[16]: True
    
    In [17]: b.isdigit()
    Out[17]: False
    
    In [18]: 
    

提交回复
热议问题