Python字符串不需要的覆盖
我似乎无法理解在python中操作字符串时为什么或如何丢失字符 字符串为:Python字符串不需要的覆盖,python,string,insert,Python,String,Insert,我似乎无法理解在python中操作字符串时为什么或如何丢失字符 字符串为:0039-7806RRVLAK 预期输出:0039-7806R-RVLAK 实际输出:0039-7806-RVLAK 代码: if temp2[4] == '-' and temp2[10] != '-': temp3 = temp2[:9] + '-' + temp2[10:] 将9更改为10: if temp2[4] == '-' and temp2[10] != '-': temp3 = tem
0039-7806RRVLAK
预期输出:0039-7806R-RVLAK
实际输出:0039-7806-RVLAK
代码:
if temp2[4] == '-' and temp2[10] != '-':
temp3 = temp2[:9] + '-' + temp2[10:]
将9更改为10:
if temp2[4] == '-' and temp2[10] != '-':
temp3 = temp2[:10] + '-' + temp2[10:]
当调用字符串时,它的str[from:to]
,因此temp2[:9]
相当于temp2[0:9]
,它只返回0-9中的字符,而不是所需的0-10