I need to write a function that takes a string (str), and two other strings (call it replace1 and replace2), and an integer (n). The function shall return a new string, where all the string inputs from replace1 in the first string (str) and replace the new string with replace1 depending on where you want the new input. I am not supposed to use built-in functions, but I can use lens (we can suppose that replace1 has the length 1). Example ( call it replaceChoice):
>>> replaceChoice(“Mississippi”, “s”, “l”, 2)
'Mislissippi'
I hope that I explained it well. Here is my attempt:
def replaceChoice(str1, replace1,n):
newString=""
for x in str:
if x=="str1":
newString=newString+replace
else:
newString=newString+x
return newString
replace2parameter in your attempt?newString=newString+replacebecause you don't passreplaceas an argumentnfor, though it looks like it's the position you want to insert (i.e. your example should replace the secondsin the string with the proposed character). Obvious homework is obvious.