0

string comparisons

when one does string comparisons in python, does it compare only the first alphabets of the strings or does it compare every single letter or words in the strings

8th Apr 2018, 6:58 AM
Doe Dare Oladimeji
Doe Dare Oladimeji - avatar
3 Answers
+ 5
It compares whole strings. In order for two strings to be equal, they have to be identical. If one is "greater" than the other one, it means it has a character of greater value at the given position of comparison - disregarding the strings' lengths. For example: 'zt' > 'abc' ---> True ('z' is compared with 'a' and the whole comparison is terminated - 'z' is greater than 'a', so the first string is greater than the second one) 'ztcax' > 'ztf' ---> False ('z' is compared with 'z', then 't' with 't' and only the third character comparison returns a definite answer)
8th Apr 2018, 8:13 AM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 3
Strings in python are compared lexicographically. They are compared by their ascii value. Yes each letter is compared.
8th Apr 2018, 8:09 AM
Mitali
Mitali - avatar
+ 1
It compares each character until the output of the comparison is determined. For example, in this code https://code.sololearn.com/cOqUR4LcU4EZ/?ref=app it'll compare each element of the words "Ana" and "Antonio" until the third one, the first different element, and then print the result of the comparison
8th Apr 2018, 8:15 AM
VĂ­ctor PĂ©rez Carrasco
VĂ­ctor PĂ©rez Carrasco - avatar