Editor settings

General settings

Save settings

Learn to program with Python

Better string replace #

Write a function that replaces a text in a string with another text while keeping the case of the first character of the word being replaced.

For example, to replace the text "Python" with "apple", the text should become "Apple".


Tests #

  • replace_text("Python is a popular programming language.", "Python", "javaScript") should return "JavaScript is a popular programming language."
  • replace_text("The Quick Brown Fox Jumps Over The Lazy Dog.", "Quick", "fast") should return "The Fast Brown Fox Jumps Over The Lazy Dog."
  • replace_text("The quick brown fox jumps over the lazy dog.", "quick", "Fast") should return "The fast brown fox jumps over the lazy dog."
  • replace_text("I have a cat and a dog. My cat's name is Whiskers.", "Whiskers", "coco") should return "I have a cat and a dog. My cat's name is Coco."
Output will be displayed here