fopp
fopp copied to clipboard
MINOR: Section 6.5 on len ignores negative indexing, might confuse readers
Describe the bug In Section 6.5, much is made about how one can find the last character of a string, saying,
Typically, a Python programmer will access the last character by combining the two lines of code from above.
lastch = fruit[len(fruit)-1]
However, in previous sections we have already seen that we can get the last character by typing fruit[-1]
. A reader may be confused, wondering what is wrong with negative indexing that this section tells them that "a Python programmer" wouldn't even use it. I think the artificiality of the example should be acknowledged.
A possible non-redundant use of the len function would be to find the character in the middle of the string. e.g. if fruit="grape"
then fruit[len(fruit)//2]
returns "a".
#126 pending pull request