How strings are compares in python like is ab greater than z or abc less than p or b < d . How to know this? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
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

5th Jun 2022, 3:41 AM
Pratik Pattnaik
Pratik Pattnaik - avatar
6 Antworten
+ 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.
5th Jun 2022, 4:20 AM
Tibor Santa
Tibor Santa - avatar
+ 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.
5th Jun 2022, 4:44 AM
Tibor Santa
Tibor Santa - avatar
+ 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
6th Jun 2022, 12:12 PM
Pratik Pattnaik
Pratik Pattnaik - avatar
+ 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!
5th Jun 2022, 2:05 PM
Lothar
Lothar - avatar
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
5th Jun 2022, 4:37 AM
Pratik Pattnaik
Pratik Pattnaik - avatar
0
In*
6th Jun 2022, 12:12 PM
Pratik Pattnaik
Pratik Pattnaik - avatar