相关疑难解决方法(0)

转换为字符串会引发错误吗?

我想知道是否转换为字符串即str(sth)可以引发异常,例如float(sth)吗?我要求知道是否有必要将我的代码包装在:

try:
    x = str(value)
except ValueError:
    x = None
Run Code Online (Sandbox Code Playgroud)

确保执行不会因转换失败而停止.

这在Python 2和3中也有所不同,因为它们的str类不同吗?

python string casting python-3.x

29
推荐指数
4
解决办法
2931
查看次数

标签 统计

casting ×1

python ×1

python-3.x ×1

string ×1