1

I have a string and I want to replace the every time after its second occurrence only.

s = "change the string in the sentence and the save"

I would like the replace the word the to hello. But except the first one.

Output should be:

change the string in hello sentence and hello save

8 Answers 8

3

I would split the string from right with the word you're going to replace use str.rsplit() function, but only split s.count('the') - 1 times.

Then, join the output list with hello:

>>> s.rsplit('the', s.count('the') - 1)
['change the string in ', ' sentence and ', ' save']

>>> 'hello'.join(s.rsplit('the', s.count('the') - 1))
'change the string in hello sentence and hello save'
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

def replace_not_first(str, word, replace):
    str_arr = str.split(word)
    return str_arr[0] + word + replace.join(str_arr[1:])

str = "change the string in the sentence and the save"
print(replace_not_first(str, 'the', 'hello'))

prints: change the string in hello sentence and hello save

Comments

1

Try the following one liner solution.

string = 'change the string in the sentence and the save'
new_string = string[:string.find('the')+3] + string[string.find('the')+3:].replace('the', 'hello')

Comments

1

I hope this will do the job.

str = str.partition('the')[0] + str.partition('the')[1] + str.partition('the')[-1].replace('the','hello')

2 Comments

str.partition('the') needs to go through the whole text and your code is wastefully doing it three times.
Do: first, the, rest = str.partition('the'); str = first + the + rest.replace('the', 'hello')
1
>>> str = "change the string in the sentence and the save"
>>> str.replace('the', 'hello')
>>> str.replace('hello', 'the',1)

1 Comment

I guess this is not correct way. coz what if the sentence is like "hello change the string in the sentence and the save". your code will replace 1st hello which is in original string.
1

This should work

string = "change the string in the sentence and the save"
the_arr = string.split("the")
print the_arr[0] + "the" + "hello".join(the_arr[1:])`

2 Comments

I don't want to change the first one value. i need to change from the second one itself
This does not work if **hello** is already in the string and is located before the first the. Such as: **hello** change the string in the sentence and the save
1

Try this:

>>> s = "change the string in the sentence and the save"
>>> s.split("the",1)[0]+"the" + s.split("the",1)[1].replace("the","hello")
'change the string in hello sentence and hello save'

Comments

0

You can split the string into parts:

string = "change the string in the sentence and the save"

splitString = string.split()
firstIndex = splitString.index('the')

part1 = ' '.join(splitString[:firstIndex+1])
part2 = ' '.join(splitString[firstIndex+1:]).replace('the','hello')

newString = part1 + ' ' + part2

Or in one line:

newString = ' '.join(['hello' if j == 'the' and i != string.split().index('the') else j for i, j in enumerate(string.split())])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.