Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RabeyaMuna/39805ccf914940b32a0b27be052e5b66 to your computer and use it in GitHub Desktop.
Save RabeyaMuna/39805ccf914940b32a0b27be052e5b66 to your computer and use it in GitHub Desktop.
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 xyzxy…
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()
k =i[-1].replace(old,new)
new_sentence=sentence[0:-len(old)]+k
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"
@ShivamSinghCodes
Copy link

replace line 7 and 8 by this
i= sentence[0:-len(old)]
new_sentence = i+new

@yagamitaiga
Copy link

image

Facing error because a double quote is not present in the output
How to resolve??

@sadjohndoe
Copy link

sadjohndoe commented Jun 19, 2022

def replace_ending(sentence, old, new):

 if sentence.endswith(old):
    i = sentence.split()
    i[-1] = new
    new_sentence = ' '.join(i)
    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"

@dtapiap
Copy link

dtapiap commented Aug 9, 2022

it is worked for me

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()
i[-1]= new
new_sentence = " ".join(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"

@daniel-n-silva
Copy link

daniel-n-silva commented Sep 21, 2022

def replace_ending(sentence, old, new):
    if old in sentence[-len(old):]:
        i = sentence[:-len(old)]
        new_sentence = i + new
        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"

@kezzyj
Copy link

kezzyj commented Oct 4, 2022

image

Facing error because a double quote is not present in the output How to resolve??

line 3 should be replaced with
if sentence.endswith(old)

@julioccorderoc
Copy link

julioccorderoc commented Oct 6, 2022

def replace_ending(sentence, old, new):
	if sentence.endswith(old):
		i = sentence.rindex(old)
		new_sentence = sentence[:i] + new
		return new_sentence
	return sentence

It works perfectly for sentences and words alike

@Alyahmedd
Copy link

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] + 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"

@narangodaNPP
Copy link

i = sentence.split()  # split sentence into list
i[-1] = new  # modify last item of the list into new one
new_sentence = " ".join(i)  # convert into string
return new_sentence  # return new sentence

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment