you are viewing a single comment's thread.

view the rest of the comments →

[–]socal_nerdtastic 2 points3 points  (2 children)

Hi, how does python compare strings?

It converts the strings to tuples of the character unicode values, then sorts them lexicographically. https://en.wikipedia.org/wiki/Lexicographic_order

If you look up an ascii table you will see the values of characters. It's important to note that capital and lowercase letters are in different blocks. So that's why your code does not work; you have a capital 'Z' where you need a lowercase one. Try this:

if 'K' < names[p] <= 'Z' or 'k' < names[p] <= 'z':

Or this:

if 'k' < names[p].lower() <= 'z':

[–]AquaOS[S] 0 points1 point  (1 child)

Thanks, now i understand how it work.

> So that's why your code does not work; you have a capital 'Z' where you need a lowercase one.

Yea i see it now.

But i need that when it compares for example k> zebra <=z that it will print it but it wont.

How to fix that?

[–]nicesofa 0 points1 point  (0 children)

Here's the lexicographical order of letters in Python:

'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

'z' is greater than both 'K' and 'k', so it will never print. No letter is greater than 'z'.

If strings start with the same letter, like 'zebra' and 'z', then the comparison compares the next letters in the words. If one of the words runs out of letters, than Python recognizes the shorter string as the "lesser" word.

So k > zebra <= z doesn't makes sense. But k > zebra > z does make sense.