in if statement. why can' t we compare two string and return a print statement? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

in if statement. why can' t we compare two string and return a print statement?

if '10'>'5': print ('hello')

7th Aug 2016, 7:44 AM
Pavatharini.M
2 Answers
+ 2
Python doesn't see that as numbers you could also try if "house" > "car" : print("hello") how should the Programm know the size of an object? But you could compare them exactly. if "5" == "5": that will work.
7th Aug 2016, 9:37 AM
Elias Grube
Elias Grube - avatar
0
You can. However string comparison is not what you think in python. The greater and less than signs will compare the order of the string rather than its actual size. Because 1 is before 5, any string starting with it will be less than any string starting with 5. Think of it as a comparison of words in a dictionary. The position of "alpha" will always be before "beta" in a dictionary, therefore its position is always less than "beta".
7th Aug 2016, 9:39 AM
Gershon Fosu
Gershon Fosu - avatar