Strange behaviour with a for loop.

Chris Angelico rosuav at gmail.com
Sat Jan 4 02:39:32 EST 2014


On Sat, Jan 4, 2014 at 5:32 PM, Sean Murphy <mhysnm1964 at gmail.com> wrote:
> So I suspect the offset number still starts at the beginning of the string and counts forward or another way to look at it you are slicing from element x to element y. If element y is less then element x, return nothing. Does this make sense?
>
> I should have used:
>
> print a[4:6])
>
> to get:
>
> t.t

Yep, it's start and end indices, not start and length. When you use a
negative number, it counts from the back:

>>> "asdf"[-1]
'f'
>>> "asdf"[-2]
'd'

> The 2nd part of my original question still stands. I will expand upon this a bit more to give more context. I want to print from the beginning of the paragraph to the end. Each paragraph ends with "\n\n\n".
>
> If I use "\n\n\n" in lines this does return true for the string. But I don't have a starting position and ending position. The list method which I mention before can be sliced by going back one element.

The "in" operator just tells you whether it's there or not; strings
have a .index() method that tells you where something can be found.
That might be what you want here!

ChrisA



More information about the Python-list mailing list