Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #70168
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: indexing in format strings |
| Date | 2014-04-11 17:45 -0400 |
| References | <HrednSzZyaoTwtXOnZ2dnUVZ8kydnZ2d@brightview.co.uk> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.9218.1397252732.18130.python-list@python.org> (permalink) |
On 4/11/2014 5:33 PM, blindanagram wrote:
> With:
>
> l = [1,2,3]
>
> this:
>
> print('{0[0]:d}..{0[2]:d}'.format(l))
>
> gives 1..3 but this:
>
> print('{0[0]:d}..{0[-1]:d}'.format(l))
>
> gives:
>
> Traceback (most recent call last):
> File "<string>", line 1, in <fragment>
> builtins.TypeError: list indices must be integers, not str
>
> which seems to me counterintuitive.
>
> I expected indexing in a format string to behave as it does elsewhere
> but this seems not to be true.
Been discussed on the tracker. Consider:
>>> '{0[-1]}'.format({'-1': 'neg int key'})
'neg int key'
Not quoting string keys within the format string introduces ambiguity
settled by 'string unless all digits, then int'.
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
indexing in format strings blindanagram <noone@nowhere.net> - 2014-04-11 22:33 +0100
Re: indexing in format strings Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-04-11 22:43 +0100
Re: indexing in format strings Terry Reedy <tjreedy@udel.edu> - 2014-04-11 17:45 -0400
Re: indexing in format strings blindanagram <noone@nowhere.net> - 2014-04-12 00:13 +0100
Re: indexing in format strings Terry Reedy <tjreedy@udel.edu> - 2014-04-11 22:44 -0400
csiph-web