今天爱分享给大家带来python字符串转为float/int【面试题详解】,希望能够帮助到大家。
>>> a = "545.2222" >>> float(a) 545.2222 >>> int(a) Traceback (most recent call last): File "", line 1, in ValueError: invalid literal for int() with base 10: '545.2222' >>> int(float(a)) 545 >>> int('544') 544
另一种,用 ast模块
>>> import ast
>>> ast.literal_eval("545.2222")
545.2222
>>> ast.literal_eval("31")
31
原文链接:https://blog.itblood.com/3899.html,转载请注明出处。

![极品采花郎V2.0.1 官方中文步兵版+存档[电脑/9.3G] [3D互动SLG/中文/动态]](/wp-content/uploads/replace/2025/10/10/d37d3d1ad377bc12dfe713425cea79ab.webp)