[Tutor] ASCII Code

Timothy M. Brauch tmbrau00@centre.edu
Mon, 28 May 2001 03:19:09 -0400


I'm just a little curious about something I find odd in Python.  Here
are two scripts that do pretty much the same thing...

---Script 1---
chars=[]
for num in range(0,256):
    chars.append(chr(num))
for item in chars:
    print item
---End Script---

---Script 2---
for num in range(0,256):
    print chr(num)
---End Script---

Both scripts have the same output.  But, if in the first script you look
at chars, you don't see the same characters that are printed.  Instead,
you see the normal 'keyboard' letters and then the (octal) code '\177',
'200', etc. for all the special characters.  Obviously Python can
display those other characters, it did when I printed them.

And, why does Python choose to display the special characters in octal?
This makes it slightly confusing to me.  For example, ord('\n'), the
carriage return, gives me 10, but if I print '\010', what I really mean
is print '\012'.  Again, these are things that I find odd, and things I
have to try and remember when I am coding.  Of course, I don't actually
use these commands very often in coding...

 - Tim