[Python-ideas] string codes & substring equality

Steven D'Aprano steve at pearwood.info
Thu Nov 28 11:53:09 CET 2013


On Wed, Nov 27, 2013 at 03:52:00PM -0800, Ethan Furman wrote:
> On 11/27/2013 03:30 PM, Chris Angelico wrote:
> >
> >Am I going to check help("".upper) to see if it can
> > also convert digits to superscript?
> 
> Heh, that would be cool.
> 
> I retract (most of) my comment.  start(end)swith is certainly not an 
> obvious name for something that can also do random substring comparisons.

startswith and endswith are not suitable for arbitrary substring 
comparisons.

That same suggestion was made on the tutor list. The OP (Denis) gave an 
example like this:

mystring = "abcde"
assert mystring[1:-1] == "bcd"

At first glance, using startswith for substring comparisons works fine:

assert mystring.startswith("bcd", 1, -1)


but alas startswith does the wrong thing:

astr = "abcdefghijklmnopqrstuvwxyz"
astr.startswith("bcd", 1, -1) == (astr[1:-1] == "bcd")
=> returns False



-- 
Steven


More information about the Python-ideas mailing list