本题只将int类型的数据转换成字符串
思路分析:通过ascii码表获得与之对应的数字字符。假如数字是8,chr(5+48)就可以得到字符串3
细节上,如果传入的字符串是0,那么返回字符串’0’
如果传入的参数是负数,需要标识记录,最后再列表里append一个’-'字符串
lst=[1,2,3],想要反转,lst=lst[::-1]
def my_str(int_value):
if int_value == 0:
return '0'
lst = []
is_positive = True
if int_value < 0:
is_positive = False
int_value = abs(int_value)
while int_value:
number = int_value%10
int_value //= 10
str_number = chr(number+48)
lst.append(str_number)
if not is_positive:
lst.append('-')
lst = lst[::-1]
return ''.join(lst)
if __name__ == '__main__':
print(type(my_str(0)))
print(my_str(123))
print(my_str(-1245))