Python Forum

Full Version: Assigning to string slice
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is this the best practice for assigning to a string slice? I realized in that case I could have used replace(), but I was thinking of the general case, e.g. I have located a pattern in the string.

>>> sf='23'
>>> s='1234'
>>> i=s.find( sf)
>>> s[:i] + s[i+len(sf)]
'14'
I'd use replace:
>>>s = s.replace('23', '')
>>> s
'14'
>>>