python:如何判断字符串中的内容是否都为数字并且把字符串转换为数字

使用str.isdigit();有两种使用方法

str.isdigit('12345') =====>True

str.isdigit('aaaaa')======>False

或者

'12345'.isdigit()=======>True

'aaaaa'.isdigit()=======>False

注意

1.使用的是str,而不是string

2.不能判断负数符

  str.isdigit('-12345')====>False

把字符串转换为数字:

  int("12345")

原文地址:https://www.cnblogs.com/mrxiaohe/p/5070132.html