Can I get a technical explanation on the following error

G

grocery_stocker

How come something like '\' causes an error? Here is what I mean.

[cdalten@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. File "<stdin>", line 1
print "test \"
^
SyntaxError: EOL while scanning string literal
I mean, isn't the '\' a character just like the letter 't'?
 
H

Hans Müller

Try this:

print "\\"

\ is the escape character, it masks the meaning of the next chararcter.

If you write print "\" python tries to print " (the meaning of " as
the string delimiter is beeing masked) and finds no closing "
This is why you got the error.

hth.

Greetings
Hans
 
D

DasIch

'\' starts a escape sequence. You need to escape '\' with a '\' to make it
work. test \
 
G

grocery_stocker

Try this:

print "\\"

\ is the escape character, it masks the meaning of the next chararcter.

If you write print "\" python tries to print " (the meaning of " as
the string delimiter is beeing masked) and finds no closing "
This is why you got the error.

So something like

"\"

changes the meaning of " ? How? Does it just shift the ASCII bit(s)?
 
C

Chris Rebert

So something like

"\"

changes the meaning of " ? How? Does it just shift the ASCII bit(s)?

No, the Python parser handles it when it parses string literals.

Cheers,
Chris
 
D

Dennis Lee Bieber

changes the meaning of " ? How? Does it just shift the ASCII bit(s)?

It does nothing to " itself... What the combination \" tells the
parser is "remove this slash and treat the following quote as part of
the string data and not as a syntax delimiter"
Traceback ( File "<interactive input>", line 1
print "\"
^
SyntaxError: EOL while scanning single-quoted string"

That was ', ", '
Traceback ( File "<interactive input>", line 1
print '\'
^
SyntaxError: EOL while scanning single-quoted string--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

Jim Garrison

And as an interesting exercise, try

print r'test \'
print r'test \\'

Because of the way raw string parsing is defined, neither of these will
pass the parser. In fact, a raw string cannot have a backslash as
its last character.
 
N

Niklas Norrthon

P

pdpi

How come something like '\'  causes an error? Here is what I mean.

[cdalten@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.
 
P

Piet van Oostrum

Jim Garrison said:
JG> And as an interesting exercise, try
JG> print r'test \'
JG> print r'test \\'
JG> Because of the way raw string parsing is defined, neither of these will
JG> pass the parser. In fact, a raw string cannot have a backslash as
JG> its last character.

Cannot have an odd number of backslashes as its last characters.
test \\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top