-1

The replace_ending function replaces the old string in a sentence with the new string, but only if the sentence ends with the old string. If there is more than one occurrence of the old string in the sentence, only the one at the end is replaced, not all of them. For example, replace_ending("abcabc", "abc", "xyz") should return abcxyz, not xyzxyz or xyzabc. The string comparison is case-sensitive, so replace_ending("abcabc", "ABC", "xyz") should return abcabc (no changes made).

def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
if ___:
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
    i = ___
    new_sentence = ___
    return new_sentence

# Return the original sentence if there is no match 
return sentence

print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", 
"donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"
Nithin
  • 137
  • 1
  • 1
  • 6

33 Answers33

6

The answer is below. the question belong to google learning of python.

def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
    if sentence.endswith(old):
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
        i = sentence.rfind(old)
        new_sentence = sentence[:i]+new
    return new_sentence

# Return the original sentence if there is no match 
return sentence
Nithin
  • 137
  • 1
  • 1
  • 6
4
def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
    if sentence.endswith(old):
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
        i = len(old)
        new_sentence = sentence[:-i]+new
    return new_sentence

# Return the original sentence if there is no match 
return sentence
boffin04
  • 41
  • 1
2
def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence
    if sentence.endswith(old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = len(old)
        new_sentence = sentence[:-i]+new
        return new_sentence
    # Return the original sentence if there is no match
    return sentence

print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"

print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"

print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"

print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April
Piotr Labunski
  • 1,638
  • 4
  • 19
  • 26
2
def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
if sentence.endswith(old):
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
    i=len(sentence) 
    l=len(old)
    new_sentence = sentence[0:i-l] + new
    return new_sentence

# Return the original sentence if there is no match 
return sentence
LuckAmilly
  • 21
  • 1
1
def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
   if sentence[-len(old):]==old:
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
      new_sentence = sentence[:-len(old)] + new
      return new_sentence

# Return the original sentence if there is no match 
   return sentence
1
def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if sentence.endswith(old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = len(sentence)-len(old)
        new_sentence = sentence[:i]+ new
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
nagyl
  • 1,644
  • 1
  • 7
  • 18
1

Above answers doesnt helped me and I just find out my own.Using a simple len() function.

def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if sentence.endswith(old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = len(old)
        new_sentence =sentence.replace("old","new")
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
    
print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"
Output:
It's raining cats and dogs
She sells seashells by the seashore
The weather is nice in May
The weather is nice in April

Akhila
  • 11
  • 3
1
def replace_ending(sentence, old, new):
     if sentence.endswith(old):
         i = sentence.rfind(old)
         new_sentence = sentence[:i]+new
         return new_sentence
     return sentence
 
1

This work for me.

def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if old in sentence.rsplit(' ', 1):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i =  sentence.rsplit(' ', 1)[0] + str(' ') + new
        new_sentence = i
        return new_sentence

    # Return the original sentence if there is no match
    return sentence

print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", 
"donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"
0
    def replace_ending(sentence, old, new):
        # Check if the old string is at the end of the sentence 
        if sentence.endswith(old):
            # Using i as the slicing index, combine the part
            # of the sentence up to the matched string at the 
            # end with the new string
            i = sentence.rfind(old) #rfind() finds the last occurrence of the substring
            new_sentence = sentence[:i]+new
            return new_sentence
    # Return the original sentence if there is no match 
    return sentence
Józef Podlecki
  • 10,453
  • 5
  • 24
  • 50
  • 1
    This does not help either the OP or a passer-by with the same predicament. Request you to add a little explanation to your solution to make it user friendly. – shuberman Jun 14 '20 at 10:30
0
def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
sen=sentence.split()
if sen[-1] == old:
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
    i = len(old)
    new_sentence = sentence[:-i]+new

    return new_sentence

# Return the original sentence if there is no match 
return sentence
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Andreas Jun 18 '20 at 04:29
0

The above answers really didn't work for me so, i spent some time and finally got a working solution. It just rechecks for the word and gets its index.

def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if sentence.endswith(old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = sentence.find(old)
        if(sentence[i+ len(old):].endswith(old)):
            j=sentence[i + len(old):].find(old)
            new_sentence=sentence[:i+ len(old)+j] + new
            return new_sentence
        new_sentence = sentence[:i] + new
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
    
print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"

0
def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if sentence.endswith(old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = len(sentence) - len(old)
        new_sentence = sentence[:i] + new
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
    
print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"

you can also see the snippet by clicking here

0
def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if sentence.endswith(old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = sentence.rfind(old)
        new_sentence = sentence[:i] + str(new)
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
0

The solution is even simpler:

def replace_ending(sentence, old, new):
    if sentence.endswith(old):
        i = -len(old)
        sentence = sentence[:i] + new
    return sentence
rudolfovic
  • 3,163
  • 2
  • 14
  • 38
0

I find another solution using".rindex".I think it's similar to using"len()".

def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if sentence.endswith (old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = sentence.rindex(old)
        new_sentence =sentence[:i]+str(new)
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
    
print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"

user3386
  • 1
  • 1
0

The first thing that came to my mind looking at this problem was the replace() method. However, unlike the split() method; there are no lsplit() and rsplit() options. So, how can we create an rreplace().

I stumbled upon another thread: rreplace()

Now, it's an easy fix.

def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
if sentence.endswith(old):
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
    new_sentence = new.join(sentence.rsplit(old,1))
    return new_sentence

# Return the original sentence if there is no match 
return sentence
print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"
MD Mushfirat Mohaimin
  • 1,966
  • 3
  • 10
  • 22
0

. i = sentence.split() . # This will split the sentence in each word as a element of list . z = i[:-1] + [new] . # This will again add all the elements of list except the last one by .using slicing function that why i used slicing from beginning to till (n-1) .new_sentence = ' '.join(z) . # This will again join the elemnets of list –

     def replace_ending(sentence, old, new):
            # Check if the old string is at the end of the sentence 
            if sentence.endswith(old):
                # Using i as the slicing index, combine the part
                # of the sentence up to the matched string at the 
                # end with the new string
                i = sentence.split()
                z = i[:-1] + [new]
        
                new_sentence = ' '.join(z)
                return new_sentence
        
            # Return the original sentence if there is no match 
            return sentence
            
        print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
        # Should display "It's raining cats and dogs"
        print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
        # Should display "She sells seashells by the seashore"
        print(replace_ending("The weather is nice in May", "may", "april")) 
        # Should display "The weather is nice in May"
        print(replace_ending("The weather is nice in May", "May", "April")) 
        # Should display "The weather is nice in April"
UVPS webs
  • 1
  • 1
  • Please don't only provide code as an answer but also provide an explanation. – Tyler2P Aug 29 '21 at 17:46
  • i = sentence.split() # This will split the sentence in each word as a element of list z = i[:-1] + [new] # This will again add all the elements of list except the last one by using slicing function that why i used slicing from beginning to till (n-1) new_sentence = ' '.join(z) # This will again join the elemnets of list – UVPS webs Aug 30 '21 at 09:06
0

Here's my solution, I experimented with a different approach. I took advantage of creating the list first and then "unwrapping it by "join" method:

def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
sentence_list = sentence.split()
if sentence_list[-1] == old:
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
    sentence_list[-1] = new
    new_sentence = " ".join(sentence_list)
    return new_sentence

# Return the original sentence if there is no match 
return sentence

print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", 
"donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 24 '21 at 20:16
0
def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence
old_index=sentence.rindex(" ")+1 
if sentence[old_index:]==old:
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
    i = old_index
    new_sentence = sentence[:i]+new
    return new_sentence

# Return the original sentence if there is no match 
return sentence
  • Please don't post only code as an answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Tyler2P Nov 13 '21 at 09:00
0
if sentence.endswith(old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = sentence.split()
        i[-1] = new
        new_sentence = " ".join(i)
        return new_sentence
Vishnu
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 25 '21 at 22:27
0

I actually did it without an endswith string

def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    loc=len(old)
    x=sentence[-loc:]

    if x==old:
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = sentence[:-loc]
        new_sentence = "{}{}".format(i,new)
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
    
print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"
0
def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
if old in sentence:
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
    i = sentence.rfind(old)
    last_txt = sentence[-(len(old)):]
    if last_txt == old:
        new_sentence = sentence[:i]+new
    else:
        return sentence
    return new_sentence
# Return the original sentence if there is no match 
return sentence
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 23 '21 at 10:08
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – MD. RAKIB HASAN Dec 23 '21 at 10:18
0
def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    endsentence= sentence.split()
    if endsentence[-1] == old:
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = endsentence[-len(endsentence):-1]
        concatenate_new_sentence = i + [new]
        new_sentence=" ".join(concatenate_new_sentence)
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
    
0
def replace_ending(sentence, old, new):

# Check if the old string is at the end of the sentence 

if sentence.endswith(old):

    # Using i as the slicing index, combine the part

    # of the sentence up to te matched string at the 

    # end with the new string

    i = sentence.rfind(old)

    new_sentence = sentence[:--i]+new

    return new_sentence

# Return the original sentence if there is no match 

return sentence
ouflak
  • 2,458
  • 10
  • 44
  • 49
0

my solution without .endswith() nor rindex():

def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
if old == sentence[-len(old):]:
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
    i = sentence[:-len(old)]
    new_sentence = i + new
    return new_sentence

# Return the original sentence if there is no match 
return sentence

Regards!

Maozsi
  • 1
  • 2
0

Because idk if there is a library in python to check the last sentence (.endswith()) so I make my code manually indexing the string from behind, and then compare it if it's the same.

def replace_ending(sentence, old, new):
    new_sentence = ""
    n = 0
    for letter in range(len(sentence)): '''this loop is used to find n index of the same word '''
        dump = sentence[(len(sentence)-(1+letter))] 
        new_sentence = dump + new_sentence '''manually create sentence from behind, 
                                           and check if it's has the same word'''
        if old in new_sentence: '''if it has, we save the number of index.'''
            n = len(sentence)-(1+letter)
            break
    splitted = sentence.split()
    i = len(splitted)
    if old in splitted[i-1]: '''check if the same word exactly the last word. If 
                               it's then replace with new word'''
        sentence = sentence[:n] + new
    return sentence
0
# Replace word endings python code( checking if the end of the sentence is same as "old" and replacing with "new")
def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    sentence = sentence.split()
    old = old.split()
    new = new.split()
    new_sentence = " "
    for word in old:
        for words in new:
            if (sentence[-1] == word):
                new_sentence = new_sentence.join(sentence[0:-1] + new)
            else:
                new_sentence = new_sentence.join(sentence)
            return new_sentence
    return sentence
print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"
    
0
def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if sentence.split()[-1]==old:
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        sentence = " ".join(sentence.split()[:-1])
        new_sentence = sentence +" "+ new
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
    
print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"
artb433
  • 1
  • 1
0

I'ts my own code and it worked correctly :

def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if sentence.endswith(old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = sentence.split()
        new_sentence = ' '.join(i[0:-1]) + " " + new
        return new_sentence
cigien
  • 57,834
  • 11
  • 73
  • 112
0
def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if sentence.endswith(old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        count=sentence.count(old)
        if (count >1):
            i =sentence.index(old)
            i= sentence.index(old,i+1)
        else:
            i=sentence.index(old)
        l=len(old)
        new_sentence =sentence[:i]+new+sentence[i+l:]
        return new_sentence


    # Return the original sentence if there is no match 
    return sentence
sarika
  • 1
  • 1
0
def replace_ending(sentence, old, new):
    if sentence.endswith(old):
        i = sentence.replace(old, new)
        return i
    return sentence
    
print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"

-1
def replace_ending(sentence,old,new):
    divide= list(sentence.split(" "))
    if divide[-1] == old:
        return ' '.join(divide[:-1])+" "+new
    else:
        return sentence
Marko Borković
  • 1,884
  • 1
  • 7
  • 22
Sush Nayak
  • 11
  • 1