在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'

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

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