|
发表于 2021-8-8 22:38:18
|
显示全部楼层
1.是相同的
2.int()函数将值转换成整数 , 如果字符串里全都是数字的话 , 是可以把它们转换成整数的
str()函数会把参数转化成字符串
- >>> s = '5201314'
- >>> int(s)
- 5201314
- >>> int('Harry')
- Traceback (most recent call last):
- File "<pyshell#2>", line 1, in <module>
- int('Harry')
- ValueError: invalid literal for int() with base 10: 'Harry'
- >>> #不是数字会报错
- >>> str(520)
- '520'
- >>>
复制代码
3.int() 可以把字符串转化成整数 , 但是字符串本身必须全是数字构成 , 小数点也不行
- >>> str(520)
- '520'
- >>> int('5201314')
- 5201314
- >>> int('520hhh')
- Traceback (most recent call last):
- File "<pyshell#6>", line 1, in <module>
- int('520hhh')
- ValueError: invalid literal for int() with base 10: '520hhh'
- >>> int('9.8')
- Traceback (most recent call last):
- File "<pyshell#7>", line 1, in <module>
- int('9.8')
- ValueError: invalid literal for int() with base 10: '9.8'
- >>> int(9.8)
- 9
- >>>
复制代码
4.相同 , 返回值都一样 |
|