Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string index out of range
#1
Here is the code that is causing IndexError...

s = "python"
print(s[8])
Output:
Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range
But this code is not occured error!
Could someone tell me why this is happening?


s = "python"
print(s[:8])
Output:
python
Reply
#2
Slices are polite?

What would you prefer, for the slice to give you as big a slice as it can, up to what you ask, or to raise an exception. The author picked the former.
Reply
#3
(Jan-13-2021, 03:27 AM)deanhystad Wrote: Slices are polite?

What would you prefer, for the slice to give you as big a slice as it can, up to what you ask, or to raise an exception. The author picked the former.

What I want to know is why the error did not occur when the range of the index is greater than the length of the string.
Reply
#4
From the python docs

Sequences also support slicing: a[i:j] selects all items with index k such that i <= k < j. When used as an expression, a slice is a sequence of the same type. This implies that the index set is renumbered so that it starts at 0.

So the slice range is a limitation on the index, not a generator of the index. When slicing 'python' the sequences is already limited to indices 0 to 5 because len('python') == 6. The slice will never test for 'python'[8] because 'python'[8] is not part of the 'python' sequence.
Reply
#5
I found the following.

From the python intro:
Quote:Degenerate slice indices are handled gracefully: an index that is too large is replaced by the string size, an upper bound smaller than the lower bound returns an empty string.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: list index out of range brunolelli 11 872 Mar-25-2021, 11:36 PM
Last Post: brunolelli
  index of range, but data prints out mrc06405j 1 326 Mar-25-2021, 07:20 PM
Last Post: buran
  IndexError: list index out of range ramu4651 2 525 Jan-24-2021, 01:45 PM
Last Post: buran
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 629 Nov-04-2020, 11:26 AM
Last Post: Aggam
  List index out of range error while accessing 2 lists in python K11 2 570 Sep-29-2020, 05:24 AM
Last Post: K11
  List index out of range error when attempting to make a basic shift code djwilson0495 4 792 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  String index out of bounds ( Python : Dict ) kommu 2 706 Jun-25-2020, 08:52 PM
Last Post: menator01
  IndexError: index out of range: 0 tomX 1 1,276 May-01-2020, 10:32 AM
Last Post: Larz60+
  String index out of range - help please DudleyDiccle 7 1,069 Mar-27-2020, 12:10 AM
Last Post: DudleyDiccle
  Get the codemap combinations but got tuple index out of range haganmao 1 672 Feb-21-2020, 02:51 AM
Last Post: michael1789

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020