String comparison

Duncan Booth duncan at
Thu Aug 22 10:35:25 CEST 2002

"Matt Gerrans" <matt_gerrans at> wrote in
news:ak19kh$qdt$1 at 

> I've been in the habit of doing something like this:
> if s1.lower() == s2.lower():
>    blah()
> I suppose this would do the trick, too:
> if re.match( '^' + s1 + '$', s2, re.I ):
>    blah()
> I'm not sure which of these is better (or maybe it is neither!),
> though, so, of course, I'd like to know what the One True Path is...

I'm sorry. Are you really serious? You don't know which is better: the one 
that is clean and readable, or the one that is obscure and doesn't work.

You re.match won't work if s1 contains any characters that have special 
meaning in regular expressions. There is an easy way around that, but I'm 
not going to tell you what it is.

For a case insensitive comparison simply do what you did first time around. 

Duncan Booth                                             duncan at
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?

More information about the Python-list mailing list