Python Forum
Assigning to string slice - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Assigning to string slice (/thread-8161.html)



Assigning to string slice - michaeljhuman - Feb-07-2018

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'


RE: Assigning to string slice - Larz60+ - Feb-08-2018

I'd use replace:
>>>s = s.replace('23', '')
>>> s
'14'
>>>