The following code is supposed to double each letter in a particular string (ex. 'hello' to 'hheelllloo') but the function is returning the "list index out of range" error to all string inputs. The problem is obviously caused by a conflict between the ever increasing length of the string with each subsequent insert and the constant value of the length of the array calculated only at the beginning of the for statement.
def double_char(str):
l = list(str)
for i in range (0,len(l)+1):
l.insert(2*i, l[2*i])
return l
I wanted to figure out if there is some way to continuously calculate the length of the list with each for statement so the insert command will function properly while still keeping the initial range of i fixed.
iand compare tolen(l). Or use regex:re.sub("(.)", "\\1\\1", str)stras a variable name.stris a python built in method.