This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: str.split() indexing issue
Type: behavior Stage: resolved
Components: Build Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: steven.daprano, tegridycode
Priority: normal Keywords:

Created on 2021-01-30 08:13 by tegridycode, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg385975 - (view) Author: Aleksandr Sigalov (tegridycode) Date: 2021-01-30 08:13
The following code ran in Google Colab...

=====================================

import sys

print (sys.version)

string = 'WORD=BIRD\nBIRD\nBIRD'

print(string.split())

print('========')

print(string.split('=')[0][0:3])
print(string.split('=')[1][0:3])

=================================

produces this...

=================================

3.6.9 (default, Oct  8 2020, 12:12:24) 
[GCC 8.4.0]
['WORD=BIRD', 'BIRD', 'BIRD']
========
WOR
BIR

=================================

Shouldn't index [0:3] give 4 chars? I looked in the docs and I could not find anything explaining this behavior.

=================================

Thanks.
msg385976 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-01-30 08:29
Hi Aleksandr,

In future, when posting what you think might be a bug, please try to cut the code down to the bare minimum needed. In this case, it doesn't matter at all that the strings you are processing come from splitting a larger string. split() has done its job, correctly, giving you a list of  substrings

    ['WORD', 'BIRD\nBIRD\nBIRD']

You then extract each item, and only then take the slice from it. So you can simplify the problem:

    string = 'WORD'
    print(string[0:3])

You ask:

"Shouldn't index [0:3] give 4 chars?"

No. It gives *three* characters. The end index is not included in the slice. Slice indexes occur *between* the characters:

    |W|O|R|D|
    0.1.2.3.4

so a slice from 0 to 3 includes only three characters, not four.
msg385977 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-01-30 08:32
Slicing is described here:

https://docs.python.org/3/tutorial/introduction.html
History
Date User Action Args
2022-04-11 14:59:40adminsetgithub: 87242
2021-01-30 08:32:15steven.dapranosetmessages: + msg385977
2021-01-30 08:29:30steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg385976

resolution: not a bug
stage: resolved
2021-01-30 08:13:38tegridycodecreate