I know I can slice a string in Python by using array notation: str[1:6], but how do I splice it? i.e., replace str[1:6] with another string, possibly of a different length?
Python strings are immutable, you need to manually:
new = str[:1] + new + str[6:]
3
votes
What about such try?
>>> str = 'This is something...'
>>> s = 'Theese are'
>>> print str
This is something...
>>> str = str.replace(str[0:7], s)
>>> print str
Theese are something...
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more