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.

Author Rin
Recipients Rin
Date 2021-09-24.16:33:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1632501204.02.0.12612984159.issue45284@roundup.psfhosted.org>
In-reply-to
Content
The `TypeError` message when a string is indexed using a string should be similar to the `TypeError` message when a list or tuple is indexed using a string.

>>> my_str = 'Rin'
>>> my_str[1:3]  # works
'in'
>>> my_str['no']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string indices must be integers

>>> my_str[slice(1, 3)]  # works with slices
'in'

Certainly it does work with slice as intended but the error message should explain that, as seen in the following

>>> my_list = [1, 2, 3]
>>> my_list['no']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not str

>>> my_tuple = (1, 2, 3)
>>> my_tuple['no']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: tuple indices must be integers or slices, not str

The error message shows `slices` are indeed an option to use when indexing a list or tuple.

Would be happy to submit a documentation PR if this minor change would be accepted.
History
Date User Action Args
2021-09-24 16:33:24Rinsetrecipients: + Rin
2021-09-24 16:33:24Rinsetmessageid: <1632501204.02.0.12612984159.issue45284@roundup.psfhosted.org>
2021-09-24 16:33:24Rinlinkissue45284 messages
2021-09-24 16:33:23Rincreate