Why is ('bcd' >= 'azd') true? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why is ('bcd' >= 'azd') true?

Played around with the string comparison, and got wrong results. Or perhaps I didn't get the concept. Also tried the lenght of the array, and strill ( 'bcd' >= 'aaaa') gives me true https://code.sololearn.com/cjOqc4NBgLsf/?ref=app

10th Jun 2017, 4:34 PM
Sina Seirafi
Sina Seirafi - avatar
5 Answers
+ 3
It's because a<b in lexicographical (that is, alphabetical) ordering. See the docs: https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted. So you have this: >>> print('abc'<'abc') False >>> print('abc'<'abd') True >>> print('abc'<'abcde') True >>> print('abc'<'abb') False >>> print('abc'<'ab') False >>> print('abc'<'aa') False
16th Jun 2017, 8:31 AM
Bogdan Sass
Bogdan Sass - avatar
+ 1
Also tested the lenght, still doesn't work as I thought it would
10th Jun 2017, 4:43 PM
Sina Seirafi
Sina Seirafi - avatar
0
Since strings, this is an array of letters, actually logical applications to them, will ponder the length of the array.
10th Jun 2017, 4:38 PM
Артём Брилёв
Артём Брилёв - avatar
0
b = 98, c = 99, d = 100; a = 97, z = 122, d = 100; I don't know why
10th Jun 2017, 4:39 PM
Andrés04_ve
Andrés04_ve - avatar
- 1
You know, it displays the value of the operator. Strings are ignored
10th Jun 2017, 4:41 PM
Артём Брилёв
Артём Брилёв - avatar