I want to replace the word 'I' in a sentence by the word 'you', and many other such examples. This is not that difficult, except that the 'I' in a word like 'Instagram' is now also being replaced - which I do not want. I would expect this is very simple but cannot find the solution (I am still inexperienced in programming). Here is my code.
s = 'I can increase my number of Instagram followers'
i = s.split()
i = [i.replace('I', 'you') for i in i]
i = [i.replace('my', 'your') for i in I]
i = " ".join(i)
This gives:
You can increase your number of younstagram followers.
But how do you get this result?
You can increase your number of Instagram followers.
EDIT
I have changed the title of the question
FROM: How to find a word in a string that is not part of another word? Python.
TO: How to replace a word in a sentence ONLY IF that word appears on its own and is not part of another word? Python
This question was marked as a duplicate of Check if string appears as its own word - Python.
However, the answers there only seem to check if a given word appears in a sentence as its own word (not as part of another word) and return a boolean. However, knowing that may be a necessary condition but is not a sufficient condition for replacing that word by something else – which is my goal. Therefore, my question may not be a duplicate of the question it is now linked with.
The following would be simple solutions, but do not work:
i = [i.replace(' I ' , 'you') for i in i] # white space left and right of 'I'
i = [i.replace('''I''', 'you') for i in i]
Any suggestions how to solve this problem?