I have two strings, s1 and s2, and I need to find if there are letters in common in both of them and make it double for example:
s1 = "Hello World"
s2 = "lo"
output = "Helllloo Woorlld"
My code:
s2="lo"
s1="Hello World"
def Str_dup(s1,s2):
s3=str()
d=dict()
for i in s2:
d[i]=True
print i
w=0
for w in range(len(s1)):
if d[s1[w]]:
s3[w]=s3[w]+s1[w]
else :
s3[w]=s1[w]
return s3
Str_dup(s1,s2)
I get an error on:
if d[s2[w]]:
IndexError: string index out of range
s1is longer thans2, so obviouslywis too big sometimes to gets2[w]. Also you can't modify a string as ins3[w]=.... You need to make a list of characters and then combine them into a string with''.join(list_of_chars).