Char to ASCII and back ?

Erik Max Francis max at alcyone.com
Fri Apr 25 11:57:16 CEST 2003


Jean-Paul Roy wrote:

> How do you transform a char (or a one-char string) into the integer
> ascii code and back ? Say to code a message.

You want ord and chr, respectively:

>>> ord('A')
65
>>> chr(65)
'A'

> The string type is not mutable in Python. Is there something like the
> StringBuffer class in Python whose instances would be mutable ?

You almost certainly want a variant of the StringIO class, either in the
StringIO (Python-only) or cStringIO (optimized C):

>>> import StringIO
>>> output = StringIO.StringIO()
>>> output.write("Hello, world.\n")
>>> output.getvalue()
'Hello, world.\n'
>>> input = StringIO.StringIO("Goodbye, world.\n")
>>> input.read()
'Goodbye, world.\n'

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ The actor is not quite a human being -- but then, who is?
\__/ George Sanders
    Bosskey.net: Unreal Tournament 2003 / http://www.bosskey.net/ut2k3/
 A personal guide to Unreal Tournament 2003.




More information about the Python-list mailing list