Answer the question
In order to leave comments, you need to log in
How to replace words in a string?
There is text:
text = "Hello lolo lo!"
I need to somehow replace the word "lo" with the word "lol"
I did it through replace () and it came out: "Hellol lollol lol".
And it is necessary that in the end it becomes: ""Hello lolo lol!""
Preferably without libraries.
Answer the question
In order to leave comments, you need to log in
Crooked, rude, the cycle is made from the bulldozer, but on the knee I propose this approach:
s = "Hello lolo lo!"
lst = s.replace('!', '').split()
print(lst)
i = int(0)
for el in lst:
if lst[i] == 'lo': lst[i] = 'лол'
i+=1
print(lst)
There are some stupid ways
text.replace('lo!', ' лол!')
text[::-1].replace('lo'[::-1], 'лол', 1)[::-1]
re.sub(r'\blo\b', 'лол', text)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question