Let's say I have the following string: 'streets are shiny.'
I wish to find every occurrence of the string 'st'
and replace it with 'ts'
. So the result should read 'tseets are shiny'
.
I know this can be done using re.sub()
or str.replace()
. However, say I have the following strings:
'st'
'sts'
'stst'
I want them to change to 'ts'
,'tss'
and 'ttss'
respectively, as I want all occurrences of 'st'
to change to 'ts'
.
What is the best way to replace these strings with optimal runtime? I know I could continually perform a check to see if "st" in string
until this returns False
, but is there a better way?