interpretation of special characters in Python

Ben Finney bignose+hates-spam at
Mon Jul 7 09:14:10 CEST 2008

TP <Tribulations at Paralleles.invalid> writes:

> Peter Pearson wrote:
> > When you tell python "print '\033[30;44m foo \033[0m'", python
> > interprets the "\033" as a single character.
> So, the python print command *can* interpret these 4-character as a
> single character.

Not "interpret", no.

It's more accurate to say that the Python *compiler* will translate
"\033" within a string literal into a single character in the compiled
executable byte code.

That is, the input to the Python interpreter will not have "\033" in
the string literal at all, but instead a single character produced by
the Python compiler at that point in the byte code.

> It would be odd if there were no possibility to do the same thing
> when the characters are (i) stored in a python variable, or (ii)
> come from the environment variables.

Since those are not the input to the Python compiler, they can't be
translated this way.

 \        “You know I could rent you out as a decoy for duck hunters?” |
  `\                                                     —Groucho Marx |
_o__)                                                                  |
Ben Finney

More information about the Python-list mailing list