0

I am starting with python and i got a challenge in my course based on string operations. I will not put the whole challenge cause i want to solve it on my own, but want to ask a question:

a = "abc"
b = "aabc"
b < a
True

How the strings are compared? b has more characters, regardless b < a is evaluated to be True

M.wol
  • 901
  • 3
  • 10
  • 21
  • 1
    Strings are compared lexicographically, which essentially means alphabetically. – iz_ Nov 19 '19 at 20:13
  • 2
    Possible duplicate of [String comparison technique used by Python](https://stackoverflow.com/questions/4806911/string-comparison-technique-used-by-python) – Fred Larson Nov 19 '19 at 20:18

2 Answers2

1

Python compares lexicographically. Find out more in the doc.

Zerg Overmind
  • 955
  • 2
  • 14
  • 28
0

When comparing strings, characters and their case are taken into account. Thus, a numeric character is conventionally smaller than any alphabetic character. The uppercase alphabetic character is conditionally smaller than the lowercase alphabetic characters. If the initial characters represent alphabetic characters in the same case, then look alphabetically. So, " aa "is less than "ba" and " ba "is less than"ca".

jashilko
  • 1
  • 1