Home python Python, IndexError: string index out of range

Python, IndexError: string index out of range

Author

Date

Category

I don’t understand how to fix the error. Typing aaaabbcaa should show a4b2c1a2 but a4bbcaa and error:

line 10, in & lt; module & gt;
  if gen [j] == gen [i]:
IndexError: string index out of range

gen = input ()
l = len (gen) -1
for i in range (0, l):
 chs = 1
 if gen [i] == int:
  continue
 for j in range (i + 1, l + 1):
  if gen [i] == chs:
   continue
  if gen [j] == gen [i]:
   chs + = 1
  elif gen [j]! = gen [i]:
   s = gen [i: j]
   s = s.replace (s [i: j], s [i] + str (chs))
   gen = s + gen [j: l + 1]
   continue
print (gen)

Answer 1

You can use a similar algorithm to convert a string

& gt; & gt; & gt; gen = "aaaabbcaa"
& gt; & gt; & gt; res = []
& gt; & gt; & gt; for c in gen:
... if res and c == res [-1] [0]:
... res [-1] [1] + = 1
... else:
... res + = [[c, 1]]
...
& gt; & gt; & gt; '' .join (''. join (str (elem) for elem in sub) for sub in res)
'a4b2c1a2'

Ie the code will look like this:

gen = input ()
res = []
for c in gen:
 if res and c == res [-1] [0]:
  res [-1] [1] + = 1
 else:
  res + = [[c, 1]]
print (''. join (''. join (str (elem) for elem in sub) for sub in res))

UPD

The problem with your code is that you change the gen line, which means that its length also changes, while your loops go along the static length of the original line. To test it, run this code and see the result:

trace = lambda: print (f'l = {l}; chs = {chs}; i = {i }; j = {j}; \ n \ t \ ts = {s}; gen = {gen} ')
gen = 'aaaabbcaa'
l = len (gen) -1
j = None
s = ''
for i in range (0, l):
 chs = 1
 trace ()
 if gen [i] == int:
  continue
 for j in range (i + 1, l + 1):
  trace ()
  if gen [i] == chs:
   continue
  if gen [j] == gen [i]:
   chs + = 1
  elif gen [j]! = gen [i]:
   s = gen [i: j]
   s = s.replace (s [i: j], s [i] + str (chs))
   gen = s + gen [j: l + 1]
   continue
print (gen)

As you can see from the last successful output of the variables, the length of the gen line is 7, i.e. the last index in it is 6, and the variable j contains the number 7. Hence the exception

l = 8; chs = 1; i = 0; j = None;
    s =; gen = aaaabbcaa
l = 8; chs = 1; i = 0; j = 1;
    s =; gen = aaaabbcaa
l = 8; chs = 2; i = 0; j = 2;
    s =; gen = aaaabbcaa
l = 8; chs = 3; i = 0; j = 3;
    s =; gen = aaaabbcaa
l = 8; chs = 4; i = 0; j = 4;
    s =; gen = aaaabbcaa
l = 8; chs = 4; i = 0; j = 5;
    s = a4; gen = a4bbcaa
l = 8; chs = 5; i = 0; j = 6;
    s = a4; gen = a4bbcaa
l = 8; chs = 6; i = 0; j = 7;
    s = a4; gen = a4bbcaa
Traceback (most recent call last):
 File "main.py", line 17, in & lt; module & gt;
  if gen [j] == gen [i]:
IndexError: string index out of range

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions