Python's string comparison is similar to Java, and also requires a comparison function, and the == symbol cannot be used. Use the cmp() method to compare two objects. If they are equal, 0 will be returned. If the former is greater than the latter, 1 will be returned. If they are less, -1 will be returned.
Example:
a = "abc" b = "abc" c = "aba" d = "abd" print cmp(a,b) print cmp(a,c) print cmp(a,d)
Return
0 1 -1
Related recommendations: "Python Video Tutorial》
Note:
The cmp function has been removed from python3.
You can use == to compare strings, and the effect is the same as the cmp function. You can also use is.
>>> a='abc' >>> b='abc' >>> a is b True >>> id(a) == id(b) True >>>
The above is the detailed content of How to compare string sizes in python. For more information, please follow other related articles on the PHP Chinese website!