Question: This is my code from part 1. It runs and works. The goal was to get eggplant to print as egg-lant. def replace_at_index(text,index=0,replacement='-'): return '%s%s%s'%(text[:index],replacement,text[index+1:])

This is my code from part 1. It runs and works.

The goal was to get eggplant to print as egg-lant.

def replace_at_index(text,index=0,replacement='-'): return '%s%s%s'%(text[:index],replacement,text[index+1:])

print(replace_at_index('eggplant',3))

7.2.8: Part 2, Replace a Letter

Write a function called replace_at_index that takes three arguments - a string, an integer (representing an index), and a string. Return a string that is the same as the first string, except with the character at the specified index replaced by the second string.

Ask the user for some text, which index to replace, and what to replace that index with (in that order!).

Print out the result of calling your function.

If the user enters the following input:

Enter a word or phrase: dog Enter an index value: 0 Enter the new letter: f

The following should be output:

fog

This is what I have so far:

word = input("Enter a word or phrase: ") index = input("Enter an index value: ") letter = input("Enter the new letter: ")

def replace_at_index(text,index=index,replacement=letter): return '%s%s%s'%(text[:index],replacement,text[index:])

print(replace_at_index(word,3))

The problem is instead of replacing the letter with the new it's putting it at the end like this:

dogf

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!