[Python] python自定义isnumber函数判断字符串是否为数字 →→→→→进入此内容的聊天室

来自 , 2019-04-02, 写在 Python, 查看 140 次.
URL http://www.code666.cn/view/832635d6
  1. ''' isnumeric.py
  2. test a numeric string s if it's usable for int(s) or float(s)
  3. '''
  4.  
  5. def isnumeric(s):
  6.     '''returns True if string s is numeric'''
  7.     return all(c in "0123456789.+-" for c in s)
  8.  
  9. # test module ...
  10. if __name__ == '__main__':
  11.     print(isnumeric('123'))      # True
  12.     print(isnumeric('-123.45'))  # True
  13.     print(isnumeric('+3.14'))    # True
  14.     print(isnumeric('$99.95'))   # False
  15. #//python/8269

回复 "python自定义isnumber函数判断字符串是否为数字"

这儿你可以回复上面这条便签

captcha