Python: String replace index

前端 未结 6 1243
面向向阳花
面向向阳花 2020-12-11 05:04

I mean, i want to replace str[9:11] for another string. If I do str.replace(str[9:11], \"###\") It doesn\'t work, because the sequence [9:11] can b

6条回答
  •  既然无缘
    2020-12-11 05:07

    Here is a sample code:

    word = "astalavista"
    index = 0
    newword = ""
    addon = "xyz"
    while index < 8:
        newword = newword + word[index]
        index += 1
        ind = index
    
    i = 0
    while i < len(addon):
        newword = newword + addon[i]
        i += 1
    
    while ind < len(word):
        newword = newword + word[ind]
        ind += 1
    
    print newword
    

提交回复
热议问题