[Tutor] Clarification on .format() Method and Negative Indices in Python
Alan Gauld
alan.gauld at yahoo.co.uk
Wed Sep 25 11:03:27 EDT 2024
On 25/09/2024 03:23, Xingyuan Shi via Tutor wrote:
> was curious as to why negative indices like {-1} are not supported in this
> method, while negative indexing works perfectly with lists and other
> sequences in Python.
Yes, it is odd. I can see why it might be useful too.
OTOH its not something I've ever looked for so maybe whoever
designed it just didn't think it necessary.
However, when it comes to design decisions you are probably
better asking on the main python mailing list as more of
the core team hang out there.
You could also try the Python dev mailing list and look for
the PEP that covers string formatting(3101). You might find
a discussion there.
Here is the original PEP:
https://peps.python.org/pep-3101/
> For instance, I attempted to use the following code:
>
> print("{-1},{-1},{-1}".format(20, "example", 3.14))
>
> I expected the placeholders {-1} to behave similarly to negative indices
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list