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

前端 未结 6 952
情书的邮戳
情书的邮戳 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:31

    Here is a short hand to do it without ast import

        try:
            if len(str(int(decdata))) == len(decdata): return 'int'
        except Exception:
            return 'not int'
    

    Of course 's' is the string you want to evaluate

提交回复
热议问题