[Tutor] Question regarding the 'chr' function

Alan Gauld alan.gauld at btinternet.com
Sun Jun 9 21:21:01 CEST 2013


On 09/06/13 19:56, Colin Clayton wrote:

> really clicks with me. If I want to make an AsciiChart function that
> returns a string that is an AsciiChart using the 'chr' function how

Since you are obviously aware that character symbols are stored on the 
computer as numeric codes then the explanation of chr() is that given a 
numeric code it returns the character symbol.

So to generate an ASCII table you print the list of codes and their 
corresponding symbol. You get the symbol by applying chr() to the codes.


You can go the other way by using ord() which takes a symbol and returns 
the code.

Try:

 >>> print( chr(65) )

 >>> print( ord('A') )

and other samples to see it in effect.

In Python chr()/ord() works with the chosen character set
not just simple ASCII...

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list