0

What is my error in this string function? Cannot figure out what I did wrong.

Return a string like s, but with all instances of orig replaced with repl.

Example:
>>> replace('Hellx wxrld', 'x', 'o')
'Hello world'
def replace(s, orig, repl):
    s.replace(orig, repl)
    return s
1
  • Strings are immutable - s.replace can't change the value of s, it can only create a new one. Commented Mar 16, 2022 at 22:29

1 Answer 1

1

The Python docs says "Return a copy of the string" which means that the s variable isn't modified, so you need to modify your code to return the replace.

def replace(s, orig, repl):
    return s.replace(orig, repl)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.