72.5k views
5 votes
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

User Meltuhamy
by
4.4k points

1 Answer

7 votes

Answer:

def replace_at_index(str, number):

new = str.replace(str[number], "-")

return new

print(replace_at_index("eggplant", 3))

Step-by-step explanation:

- Create a function called replace_at_index that takes a string and an integer

- Initialize a new variable called new, that will hold the new string

- Replace the character at given index with dash using replace function, it takes two parameters: the first is the character we want to replace, the second is the new character.

- Return the new string

- Call the function with the required inputs