how to compare two strings?

Martin v. Loewis martin at v.loewis.de
Tue Feb 12 10:20:55 CET 2002


Paul Rubin <phr-n2002a at nightsong.com> writes:

> Is there any built-in function in Python for comparing two strings
> alphabetically?

Can you specify "alphabetically" more precisely? Do you mean
"lexicographically, by character ordinal value" (which is what your
scmp code suggests)? If so, the builtin cmp() function will do;
it compares strings just fine.

Or do you mean "according to the locale's conventions"? That would be
locale.strcoll. Notice that for sorting according to the locale's
conventions, it is more efficient to invoke strxfrm for each string,
and then sort by the transformed results.

Regards,
Martin



More information about the Python-list mailing list