how to compare two strings?

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

Paul Rubin <phr-n2002a at> 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.


More information about the Python-list mailing list