Python: String replace index

前端 未结 6 1246
面向向阳花
面向向阳花 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:28

    You can use join() with sub-strings.

    s = 'cdabcjkewabcef'
    sequence = '###'
    indicies = (9,11)
    print sequence.join([s[:indicies[0]-1], s[indicies[1]:]])
    >>> 'cdabcjke###cef'
    

提交回复
热议问题