"banana
" <
"orange
" if I compare this two strings in Python-3
they return True
. But I see that both strings have 6 characters each.
Again when I try with "banana
" < "Orange
" returns
False
though they are also still consisted of 6 characters each. I can't understand them? Could you please help me to understand them in a simple way?

- 100,966
- 191
- 140
- 197

- 301
- 1
- 2
- 13
-
1They are both False; banana is superior to orange. – OverLordGoldDragon Nov 16 '19 at 05:02
4 Answers
As pointed out in the question I linked to in my comment the strings are compared in order.
print(ord('b'))
print(ord('o'))
98
111
98 < 111 == true
print(ord('b'))
print(ord('O'))
98
79
98 < 79 == false

- 6,594
- 3
- 39
- 62
This is because strings are compared based on their ACSII values character by character. When you use lowercase 'b' (#98) is less than 'o' (#111) and comparison evaluates to True. In the second case the capital 'O' is number 79 and as a result it evaluates as False.

- 3,821
- 2
- 29
- 44
It is called 'Dictionary order'. It compares strings in the way a dictionary contains words. 'b' < 'o' that is whay "banana" < "orange".
If it would be "bbbba" and "bbbbb" then it would be "bbbba"< "bbbbb". If it would be "zzz" and "bbbb" then it would be "zzz" > "bbbb"

- 280
- 1
- 4
- 17
When two strings are being compared, they do so character by character, starting from their first (leftmost) character up to their last (rightmost) character. Each of this chars are compared in accordance to their ASCII code, if one char is found to have a smaller code then that string is deemed smaller (not to be taken in a length sense, but rather in a lexicographical sense), the same goes for a greater code, if both codes are the same then the comparison takes the next character of each string and compares them again, and so on.
Take banana
and orange
and let's compare them. Their first char is b
and o
, with codes of 98
and 111
respectively, since 98 < 111
, then b < o
and therefore, banana < orange
. However, banana > Orange
because b > O
since their codes, 98 > 79
. In fact banana > Banana
.

- 2,864
- 1
- 12
- 21