在 Python 中,可以使用 int() 函数将字符串转换为整数。

例如:

num_str = '123'
num_int = int(num_str)
print(num_int)  # 输出 123

需要注意的是,如果字符串无法转换为整数,会抛出 ValueError 异常。例如:

num_str = 'abc'
num_int = int(num_str)  # 抛出 ValueError: invalid literal for int() with base 10: 'abc'
Python 字符串转整数:int() 函数使用指南

原文地址: https://www.cveoy.top/t/topic/n7wn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录