Comparing two characters in Python

Benny K :

Is comparing two characters (that is one character str) in Python (3.x if that matters) well defined? or do I have to make an explicit conversion?

In other words, is:

'a' > 'b'

the same as:

ord('a') > ord('b')

Ondrej K. :

When not sure, check the docs:

Strings (instances of str) compare lexicographically using the numerical Unicode code points (the result of the built-in function ord()) of their characters.

So yes, the behavior is well defined.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=10230&siteId=1
Recommended