今天爱分享给大家带来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
原文链接:http://blog.itblood.com/3899.html,转载请注明出处。

![我的反乌托邦机器人女友v0.90.16 官中版[PC+安卓/1.07G/更新]Factorial Omega: My Dystopian Robot Girlfriend [v0.90.16 Fix] [精品SLG/汉化/动态]](/wp-content/uploads/replace/2025/10/22/cafb5c001f899dfcaf16d358b74897f8.webp)