Bizarre behavior of the 'find' method of strings

M

MRAB

Greetings, folks,

I am using python 2.7.2. Here is something I got:
a = 'popular'
i = a.find('o')
j = a.find('a')
a[i:j]
'opul'

Well, I expected a[i:j] to be 'opula', and can't think of any reason
why this is not happening. So, can anybody help me out about this?
Thank you very much.
Python uses half-open ranges, which means that the start position is
inclusive and the end position is exclusive.

This means that a[i:j] returns the string starting at position i and
extending upto, but excluding, position j.

The reason that Python uses half-open ranges is that experience (with
the language Mesa) has shown that it results in fewer programming
errors that the alternatives.
 
C

Chris Angelico

Python uses half-open ranges, which means that the start position is
inclusive and the end position is exclusive.

Or if you prefer: Python identifies positions between characters,
rather than characters. And I agree that it's better than the
alternative - in fact, I wish other systems could work the same way
(eg Bible references). If you think about position 0 being the very
beginning of the string, and position len(s) being the very end, then
you slice the string from position to position and take the characters
between. When you index the string for a particular character, you aim
at a position and take the character after it.

Chris Angelico
 
S

Steven D'Aprano

Greetings, folks,

I am using python 2.7.2. Here is something I got:
a = 'popular'
i = a.find('o')
j = a.find('a')
a[i:j]
'opul'

Well, I expected a[i:j] to be 'opula', and can't think of any reason why
this is not happening. So, can anybody help me out about this? Thank you
very much.

Your subject line says:

Bizarre behavior of the 'find' method of strings

What makes you think this is a problem with the find method?

As a programmer, you should be able to isolate where the problem *actually*
occurs:
5

So there is no problem with the find method: it correctly finds the index
(starting at zero, not one) of the first (going left-to-right) matching
substring.

Now, if you take a slice, you get an unexpected (to you) result:
'opul'

It doesn't matter where the 1 and 5 come from. The slice can't tell that
they were the output of find, or how they were generated:
'opul'

Now that you have isolated the actual problem, you can ask a better
question:

"Why does slicing not work the way I expect?"

Answer: because Python uses half-open slices, where the end parameter is not
included. The reason for that is that experience with other languages shows
that it leads to fewer "off-by-one" errors.

See also:

http://mail.python.org/pipermail/tutor/2010-December/080592.html
http://en.wikipedia.org/wiki/Off-by-one_error
 
C

Chris Rebert

Greetings, folks,

I am using python 2.7.2. Here is something I got:
a = 'popular'
i = a.find('o')
j = a.find('a')
a[i:j]
'opul'

Well, I expected a[i:j] to be 'opula', and can't think of any reason why
this is not happening. So, can anybody help me out about this? Thank you
very much.
"Why does slicing not work the way I expect?"

Answer: because Python uses half-open slices, where the end parameter is not
included. The reason for that is that experience with other languages shows
that it leads to fewer "off-by-one" errors.

See also:

http://mail.python.org/pipermail/tutor/2010-December/080592.html
http://en.wikipedia.org/wiki/Off-by-one_error

And further:
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

Cheers,
Chris
 
J

Jim

Thanks for all you guys. Now I got it.

To Steven,
I was in a little rush when I put this post. But you are right. It's not the find method's problem.
 
J

Jim

Thanks for all you guys. Now I got it.

To Steven,
I was in a little rush when I put this post. But you are right. It's not the find method's problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top