By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,223 Members | 1,371 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,223 IT Pros & Developers. It's quick & easy.

String backslash characters

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
PD wrote:
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'.

Jul 18 '05 #2

P: n/a
PD wrote:
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:
'\378' '\x1f8' '\37' + '8'

'\x1f8'
Jul 18 '05 #3

P: n/a
'\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'

On 23 Dec 2004 20:53:13 -0800, PD <al*****@gmail.com> wrote: 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

--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #4

P: n/a
PD wrote:
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'".
--
Benji York
be***@benjiyork.com

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.