Reputation: 21
If a="formula"
and b="formulab
" are two strings, how can the function compare which string comes after the other and return a true or false value?
We know string b
will come after, but how to determine that using the function?
def alphabet_order(a,b):
if len(a) > len(b):
return True
else:
return False
I am getting the length of the strings, but how can I sort the strings lexicographically and compare them?
Upvotes: 0
Views: 762
Reputation: 5694
Python compares strings lexicographically based on their order in the ascii table for alphabet (where each letter is essentially assigned a value).
Here is a link to the numeric order: https://www.asciitable.com/
So you now have two options:
len()
).Here is an example of the value comparison:
a = 'hello'
b = 'world'
if a > b:
print('a > b')
else :
print('a < b')
which returns this:
a < b
because "hello comes" before "world" in that ordering.
You can wrap the above into a function.
Upvotes: 1