脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 解决Python的str强转int时遇到的问题

解决Python的str强转int时遇到的问题

2021-01-29 00:17孔天逸 Python

下面小编就为大家分享一篇解决Python的str强转int时遇到的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

<strong>数字字符串前后有空格没事:

?
1
2
>>> print(int(" 3 "))
3

但是下面这种带小数点的情况是不可取的:

?
1
2
3
4
>>> print(int("3.0"))
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3.0'

这种字符串强转float没问题

?
1
2
>>> print(float("3.0"))
3.0

以上这篇解决Python的str强转int时遇到的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_30242609/article/details/68948659

延伸 · 阅读

精彩推荐