Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String indexing - Why S[0][0] works and S[1][1] fails?

Suppose I create a string:

>>> S = "spam"

Now I index it as follows:

>>> S[0][0][0][0][0]

I get output as:

>>> 's'

But when i index it as:

>>> S[1][1][1][1][1]

I get output as:

Traceback (most recent call last):
File "<pyshell#125>", line 1, in <module>
L[1][1][1][1][1]
IndexError: string index out of range

Why is the output not 'p'?

Why also it is working for S[0][0] or S[0][0][0] or S[0][0][0][0] and not for S[1][1] or S[1][1][1] or S[1][1][1][1]?

like image 650
Codessci Avatar asked Nov 26 '15 07:11

Codessci


People also ask

How do you fix a string index out of the range?

The “TypeError: string index out of range” error is raised when you try to access an item at an index position that does not exist. You solve this error by making sure that your code treats strings as if they are indexed from the position 0.

Why is my string index out of range?

The string index out of range means that the index you are trying to access does not exist. In a string, that means you're trying to get a character from the string at a given point. If that given point does not exist , then you will be trying to get a character that is not inside of the string.

Does indexing work on strings?

Because strings, like lists and tuples, are a sequence-based data type, it can be accessed through indexing and slicing.

What do the index numbers of 0 and 1 mean for string handling in Python?

String indexing in Python is zero-based: the first character in the string has index 0 , the next has index 1 , and so on. The index of the last character will be the length of the string minus one. For any non-empty string s , s[len(s)-1] and s[-1] both return the last character.


2 Answers

The answer is that S[0] gives you a string of length 1, which thus necessarily has a character at index 0. S[1] also gives you a string of length 1, but it necessarily does not have a character at index 1. See below:

>>> S = "spam"
>>> S[0]
's'
>>> S[0][0]
's'
>>> S[1]
'p'
>>> S[1][0]
'p'
>>> S[1][1]
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    S[1][1]
IndexError: string index out of range
like image 106
El'endia Starman Avatar answered Oct 31 '22 05:10

El'endia Starman


The first index ([0]) of any string is its first character. Since this results in a one-character string, the first index of that string is the first character, which is itself. You can do [0] as much as you want and stay with the same character.

The second index ([1]), however, only exists for a string with at least two characters. If you've already indexed a string to produce a single-character string, [1] will not work.

>>> a = 'abcd'
>>> a[0]
'a'
>>> a[0][0]
'a'
>>> a[1]
'b'
>>> a[1][0][0]
'b'
>>> a[1][1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
like image 22
TigerhawkT3 Avatar answered Oct 31 '22 04:10

TigerhawkT3