Python 字符串不需要的覆盖

Python String unwanted overwrite

我似乎无法弄清楚为什么或如何在 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 = temp2[:10] + '-' + temp2[10:]

当你调用一个字符串它的 str[from:to] 时,所以 temp2[:9] 等同于 temp2[0:9] 并且它只会 return 从 0-9 的字符而不是所需的0-10