Skip to content

Instantly share code, notes, and snippets.

View ShivamSinghCodes's full-sized avatar
🎯
Focusing

Shivam Singh ShivamSinghCodes

🎯
Focusing
View GitHub Profile
@ShivamSinghCodes
ShivamSinghCodes / pythoncrash.py
Created January 31, 2022 19:19
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.index(old)
i= sentence[0:-len(old)]
new_sentence = i+new
return new_sentence