La fonction Remplacer_ending remplace l'ancienne chaîne d'une phrase avec la nouvelle chaîne, mais uniquement si la phrase se termine par l'ancienne chaîne. S'il y a plus d'une occurrence de l'ancienne chaîne dans la phrase, seule celle à la fin est remplacée, pas toutes. Par exemple, remplaille_end ("ABCABC", "ABC", "XYZ") doit renvoyer ABCXYZ, NON XYZXYZ OU XYZABC. La comparaison des chaînes est sensible à la casse, de sorte que remplace_end_inge ("abcabc", "ABC", "XYZ") devrait renvoyer abcabc (aucune modification apportée).
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"
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
def replace_ending(sentence, old, new):
if sentence.endswith(old):
i = sentence.rfind(old)
new_sentence = sentence[:i]+new
return new_sentence
return sentence
Les réponses ci-dessus ne m'ont pas aidé et je viens de trouver ma propre fonction len () simple ().
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
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
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"
Les réponses ci-dessus n'ont vraiment pas fonctionné pour moi, j'ai passé un peu de temps et j'ai enfin eu une solution de travail. Cela revient simplement pour le mot et obtient son 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"