String backslash characters

P

PD

Hello,

I am new to python, but i am quite curious about the following.

suppose you had

print '\378'

which should not work because \377 is the max. then it displays two
characters (an 8 and a heart in my case...). What else does'nt quite
make sense is that if this is an octal why is an 8 accepted?

for instance is 378 really 11, 111, 1000 which is then the two
characters: <00000001>,<1111000>. And why is this accepted?

I apologize if this has been discussed or if it is obvious. I would
appreciate it if someone could clear me up.

Yours,
PD
 
B

Binu K S

'\378' becomes a two character string. The first character is '\37'
and the second character is '8'.
str = '\378'
str[0] '\x1f'
str[1] '8'

Hello,

I am new to python, but i am quite curious about the following.

suppose you had

print '\378'

which should not work because \377 is the max. then it displays two
characters (an 8 and a heart in my case...). What else does'nt quite
make sense is that if this is an octal why is an 8 accepted?

for instance is 378 really 11, 111, 1000 which is then the two
characters: <00000001>,<1111000>. And why is this accepted?

I apologize if this has been discussed or if it is obvious. I would
appreciate it if someone could clear me up.

Yours,
PD
 
B

Benji York

PD said:
I am new to python, but i am quite curious about the following.

print '\378'

which should not work because \377 is the max. then it displays two
characters (an 8 and a heart in my case...). What else does'nt quite
make sense is that if this is an octal why is an 8 accepted?

It would appear that the parser reads \377 as a single character and
\378 as two (\37 and the "8" character). I'm somewhat surprised you're
seeing a heart and an 8. What OS/language combination are you using?
If you're using English Windows you can get a heart and an "8" with
"print '\38'".
 
D

Dan Bishop

PD said:
Hello,

I am new to python, but i am quite curious about the following.

suppose you had

print '\378'

which should not work because \377 is the max. then it displays two
characters (an 8 and a heart in my case...). What else does'nt quite
make sense is that if this is an octal why is an 8 accepted?

Because 8 isn't an octal digit, so it's not part of the escape
sequence, but a separate character. It's just like

print 'This string\47s escape sequence does not include the s.'

Your example displays two characters because it is two characters:
'\037' and '8'.
 
L

Leif K-Brooks

PD said:
Hello,

I am new to python, but i am quite curious about the following.

suppose you had

print '\378'

which should not work because \377 is the max. then it displays two
characters (an 8 and a heart in my case...). What else does'nt quite
make sense is that if this is an octal why is an 8 accepted?

It's not doing quite what you think. The '\37' is seen as octal, but
since '8' isn't a valid octal digit, it's seen as the literal character '8'.

In other words, these two lines are equivalent:'\x1f8'
 

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
474,214
Messages
2,571,112
Members
47,706
Latest member
HFWTina07

Latest Threads

Top