0

Tell me one thing can string be compared by the values in it

Suppose a string has value 5 and other has 7 then if we compare them by length they are same but not by value, but when we compare them they are compared by values inside it why.....

13th May 2019, 6:22 AM
Nikhil Tale
Nikhil Tale - avatar
7 Answers
+ 3
Strings are compared lexicographically (like words in a dictionary). '5' < '62' < '7' < '732' < '79'
13th May 2019, 6:47 AM
Anna
Anna - avatar
+ 3
Melih Melik Sonmez The question is about python
13th May 2019, 6:50 AM
Anna
Anna - avatar
+ 1
Strings are Text and thus just some chars (including special chars, numbers, letters). Which of these can be represented and how is defined by the encoding of your string (ASCII, UTF8, ...). That becomes important e.g. for localization, file import, network communication and so on. In general: never store numbers as strings if they have a semantic (meaning) in your code. Assemble outputs only in your view (model).
13th May 2019, 7:33 AM
Daniel Adam
Daniel Adam - avatar
0
There are more than one comparison for variables. = is for value assing == type of variable is (===) strict comparison of variable and its value (Unlike the double equals operator "==", the "is" operator does not match the values of the variables, but the instances themselves.) x = [1,2,3] y = [1,2,3] print(x == y) # Prints out True print(x is y) # Prints out False So learning thus comparisons is sometimes confusing but relax I will share code for you son you can understand better ! Important ! also python 2 and python3 behaives different for this a = "Anna" b = "{}".format("Anna") print (a , b) print (a is b , a == b) please be sure about this !
13th May 2019, 6:49 AM
Melih Melik Sonmez
Melih Melik Sonmez - avatar
0
If you want to compare two strings for their numeric values, you have to convert them into numbers. x, y = '561', '71' print(int(x)==int(y))
13th May 2019, 6:55 AM
HonFu
HonFu - avatar
0
@Anna I fixed my reply please re-check
13th May 2019, 7:32 AM
Melih Melik Sonmez
Melih Melik Sonmez - avatar