New submission from Ganesh Kumar <ganesh-ku...@live.com>:

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.

----------
components: Library (Lib)
messages: 402577
nosy: Rin
priority: normal
severity: normal
status: open
title: Better `TypeError` message when a string is indexed using a string
type: enhancement
versions: Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue45284>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to