Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 27th International Conference Dasfaa 2022 Virtual Event April 11 14 2022 Proceedings Part 2 Lncs 13246

Authors: Arnab Bhattacharya ,Janice Lee Mong Li ,Divyakant Agrawal ,P. Krishna Reddy ,Mukesh Mohania ,Anirban Mondal ,Vikram Goyal ,Rage Uday Kiran

1st Edition

3031001257, 978-3031001253

More Books

Students also viewed these Databases questions