How can I splice a string?

Nevermind. Thought there might be a built in function. Wrote this instead:

def splice(a,b,c,d=None):
    if isinstance(b,(list,tuple)):
        return a[:b[0]]+c+a[b[1]:]
    return a[:b]+d+a[c:]

>>> splice('hello world',0,5,'pizza')
'pizza world'

>>> splice('hello world',(0,5),'pizza')
'pizza world'

Leave a Comment