[Tutor] comparing strings

Edward Martinez edwardcru1 at aol.com
Fri Feb 25 08:53:49 CET 2011


On 02/24/11 02:56, Dave Angel wrote:
> On 01/-10/-28163 02:59 PM, Edward Martinez wrote:
>> On 02/23/11 19:29, Corey Richardson wrote:
>>> On 02/23/2011 10:22 PM, Edward Martinez wrote:
>>>> Hi,
>>>>
>>>> I'm new to the list and programming.
>>>> i have a question, why when i evaluate strings ie 'a'> '3' it reports
>>>> true, how does python come up with that?
>>> Welcome! As far as I know, it compares the value of the ord()'s.
>>>
>>>>>> ord('a')
>>> 97
>>>>>> ord('3')
>>> 51
>>>
>>> This is their number in the ASCII system. You can also do this:
>>>
>>>>>> chr(97)
>>> 'a'
>>>>>> chr(51)
>>> '3'
>>>
>
> A string is effectively an array of characters.  Each one may be ASCII 
> or Unicode or other, depending partly on your Python version.
>
> Each character has an ord() between 0 and 255, or between 0 and 65535. 
> Except for some values below 0x20  (eg. tab, newline), these are 
> printable.  So you can make a chart for your own system with a fairly 
> simple loop.
>
> Comparison is done left to right on the two strings, comparing one 
> character at a time.  If there are no control characters, this 
> approximates what a dictionary order would do.  But notice that all 
> the capital letters appear before any of the lowercase characters. And 
> that if you have accented characters, they're generally nowhere near 
> the unaccented versions.
>
> One other point:  if one string begins with all the characters in the 
> other (eg. 'cat' and 'catatonic'), the longer string is then 
> considered "greater".
>
> DaveA
>
>
     Thanks for the reply. i now understand that python uses either 
ASCll or Unicode to compare and to do other things
    Regards,
    Edward


More information about the Tutor mailing list