Benny K
Benny K

Reputation: 2087

Comparing two characters in Python

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')

Upvotes: 3

Views: 361

Answers (1)

Ondrej K.
Ondrej K.

Reputation: 9664

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.

Upvotes: 5

Related Questions