不使用内置函数,如何将字符串转换为整形
def convert_to_int(str):
s = 0
for i in str:
s *= 10
tmp = ord(i) - ord('0')
s += tmp
return s
val = convert_to_int("1234")
print(val, type(val)) #1234 <class 'int'>
本文介绍了一种不使用内置函数的手动方法,将字符串转换为整型数值。通过遍历字符串中的每个字符,利用ASCII码值计算出整数值。
689

被折叠的 条评论
为什么被折叠?



