Home python error Indexerror: String Index Out of Range

error Indexerror: String Index Out of Range

Author

Date

Category

How to output from the cycle only one (in this example 5th) number?
I got an error

& gt; Print (k [5])
Indexerror: String Index Out of Range

Answer 1, Authority 100%

for i in range (1,101):
  K = STR (I)

So, first the i value>will 1 and the k value>will 1 ".

Now K Length of the length 1 . The string symbols are indexed from zero, which means k [0] will be “1” and the use of positive indexes (since k [1] k [2] , …) meaningless.

print (k [5])

What is k [5] ? This is the sixth character of the k line. But this symbol does not exist – in the k line only one symbol! here and mistake.


Answer 2, Authority 100%

Solution:

Try to replace “print (k [5])” on “if i == 5: Print (k)”

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