0
How strings are compares in python like is ab greater than z or abc less than p or b < d . How to know this?
Please tell me how to basically conoare strings with each other logically
6 odpowiedzi
+ 6
First, strings are character sequences, and the value of a character that is used for the default comparison, is it's position in the ASCII / Unicode character table.
You can see this value by using the ord() function:
print(ord('1')) #49
print(ord('A')) #65
print(ord('a')) #97
print('A' < 'a') #True
print('1' < 'a') #True
When comparing strings of different length, it goes character by character, and something is bigger than nothing.
Experiment with it, but I am sure it will make sense for you.
+ 5
If you compare 'hello' and 'help' then which one is bigger?
It goes character by character.
First 3 letters are the same, so far they are equal. But p > l so at this point 'help' is bigger. It does not matter, how many other characters each word has, or what is their value. Comparison is decided by the first character that is different.
+ 2
Lothar yes Tibor Santa cleared my doubt totally
I wanted to know which is bigger if length of word is diff like
Pure Purity
Core Cored
Etc
+ 1
Pratik Pattnaik ,
can you please demonstrate (by showing a sample) your comment:
"Nicely explained , one more thing if string is off bigger length then it will add all the ASCII unicode attached to each character right"
thanks!
0
Nicely explained , one more thing if string is off bigger length then it will add all the ASCII unicode attached to each character right
0
In*