how to compare two strings?

Brian Quinlan brian at sweetapp.com
Tue Feb 12 09:23:44 CET 2002


>>> print cmp.__doc__
cmp(x, y) -> integer

Return negative if x<y, zero if x==y, positive if x>y.
>>>

> Is there any built-in function in Python for comparing two strings
> alphabetically?  Something suitable for passing as the optional
> comparison function for the list "sort" method.  I could code up
> something like
> 
>   def scmp(a,b):
>     if a<b: return -1
>     if b<a: return 1
>     return 0
> 
> but it's pretty ugly to need to do that.  I've looked in the docs but
> don't see any obvious counterpart to perl's lozenge (<=>) operator.
> Is there one?
> 
> Thanks.
> --
> http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list