Can I get a technical explanation on the following error

pdpi pdpinheiro at
Mon May 25 05:47:14 EDT 2009

On May 24, 6:41 pm, grocery_stocker <cdal... at> wrote:
> How come something like '\'  causes an error? Here is what I mean.
> [cdalten at localhost ~]$ python
> Python 2.6.2 (r262:71600, May  3 2009, 17:04:44)
> [GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.>>> print "test \"
>   File "<stdin>", line 1
>     print "test \"
>                  ^
> SyntaxError: EOL while scanning string literal
> I mean, isn't the '\' a character just like the letter 't'?

Ask yourself: 'How would I tell Python to print the " character when
it's used as the string delimitation character?'. Unlike us (you
probably didn't even blink at the usage of ' in "it's" versus its
usage as quote delimiters in that sentence), computers can't quite
tell usage from context. So you have to come up with a way to express
ourselves. Many languages use the \ character as an escape character,
which modifies the meaning of the next character in a string. \n, \t,
\" are all common. Other languages use other conventions -- I program
ABAP professionally, and it uses ' as the string delimiter, and '' as
a literal -- so a string with one single ' is expressed as ''''.

The python interpreter isn't doing any bit magic here, it's just
reading a \ character followed by a " character and changing its
interpretation of " appropriately.

More information about the Python-list mailing list