trying to replace all instances of a given character in a string with a new character. The following is my code:
def main():
s='IS GOING GO'
x='I'
y='a'
rep_chr(s,x,y)
def find_chr(s,char):
i=0
for ch in s:
if ch==char:
return (i)
break
i+=1
return -1
def rep_ch(s,x,y):
result=""
for char in s:
if char == x:
print(result=result+ y )
else:
return char
main()
Edited the code, but it is still replacing the first 'I' with 'a' and ignoring the second 'I'. Any suggestion?
range(len(s1))instead ofrange(s1).rangeis a python method :)[]operator is a method, since to use it it calls the__getitem__or__getslice__methods internally.