• Register
1 vote
24 views

Problem:

I was discovering String in python. Suddenly I stuck here by getting “indexerror: string index out of range” this error message.

def print_letters(string):
  count = 0
  newstring = []
  while count <= len(string):
    m = string[count]
    count +=1
    print m
print_letters("blanks")  

Do you have any idea? I’d appreciate it if you figure the error in my program.

12 7 7
15,250 points

1 Answer

2 votes
 
Best answer

Solution:

Hello Gavin,

According to your program, you wrote wrong here:

while count <= len(string)

Imagine you have a String length of 7 and its index is 6. So you can’t write the value equal to the String length because Strings are zero-indexed based.

Please change the condition. The program should run with no error .

13 9 6
94,240 points

Related questions

1 vote
1 answer 15 views
15 views
Problem: I am new in python programming and I have been following a course where I have been instructed to do an assginement to remove all occurrences of a character in a list python. So I write this code: Numbs = [11, 22, 11, 33, 11, 44, 11, 55] a = 11 print (" ... main.py", line 8, in <module> if(Numbs[i]==a): IndexError: list index out of range Can anyone help me out to solve this problem?
asked Apr 6 LizzyM 6.1k points
1 vote
2 answers 52 views
52 views
Problem: I am trying to adopt the python programming language. Learning it step by step and practicing daily. I&rsquo;ve written simple code that contains a list and run an iteration on it but it shows: python indexerror: list assignment index out of range m = [1, 2, 3, 5, 8, 13] n = [] o = 0 for l in m: n[o] = l o += 1 print n Where did I wrong? Any Idea?
asked Mar 30 Gavin 15.3k points
0 votes
1 answer 25 views
25 views
I wrote a code in python : l = [8, 5, 4] total = l[0] + l[1] print (total) def count(a): total = 0 for i in a: total+= a[i] return total print (count(n)) but it has some error: File "main.py", line 11 return total ^ SyntaxError: 'return' outside function What is this error causing for?
asked Mar 15 LizzyM 6.1k points
1 vote
1 answer 30 views
30 views
Problem: &ldquo;indexerror: tuple index out of range&rdquo; what does it mean? Why I am getting this error? Please shed some light on this if possible. Gratitude.
asked Mar 7 Gavin 15.3k points
0 votes
2 answers 162 views
162 views
Problem : I am new to string handling & getting below error while trying to execute a program java.lang.stringindexoutofboundsexception: string index out of range: 0
asked Oct 23, 2019 peterlaw 6.9k points