Question: This is in Python! Write a function named replace_at_index that takes a string and an integer. The function should return a new string that is
This is in Python!
Write a function named replace_at_index that takes a string and an integer. The function should return a new string that is the same as the old string, EXCEPT with a dash in place of whatever character was at the index indicated by the integer.
Call your function on the word eggplant and the index 3, like this:
s = replace_at_index("eggplant", 3) You should then print the value of s, which should display:
egg-lant
These are two versions of what I've tried to do but I'm getting an error message for line 1.
Version 1:
def replace_at_index("eggplant", s[3]): s = ("eggplant") s[3] = ("-") s = ("egg" + s[3] + "lant") print s print (replace_at_index("eggplant", 3)
Version 2:
s = ("eggplant") def replace_at_index("eggplant", s[3]): print(replace_at_index[:2] + "-" + replace_at_index[4:]) s = replace_at_index("eggplant", [3]) print s
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
