How to do string replacement character with function?


I am trying to create a function that takes 3 string parameters and replaces the characters.

The first parameter is the word, the second is the letter I want to replace, and the third is the letter I want it replaced with. This has to work for any string.

Here is the code I have so far:

import string

def replace_letter(phrase,l,r):
    phrase = f"{phrase}"
    result = phrase.replace("l", "r")
    return result



I just need the function to take the string without input, so in this form

>Solution :

Here is a commented & corrected version of your code:

#import string # not used

def replace_letter(phrase,l,r):
    #phrase = f"{phrase}"         # useless
    result = phrase.replace(l, r) # use variable names, not strings
    return result
    #print(result)  # not evaluated (after return)


output cot

Leave a ReplyCancel reply