Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How '\a' equal to '\7' in python?

Tags:

python

a = '\a'
>>> b = '\7'
>>> a == b
True
>>> 

How can a and b be equal? Can someone give the reason?

like image 574
shiva Avatar asked Jun 09 '12 12:06

shiva


3 Answers

\a is escaped character sequence for control character BEL (a for alert). The character's ASCII code is also happened to be 7, which matches the octal value in the escape sequence \7.

References:

http://en.wikipedia.org/wiki/Bell_character

http://docs.python.org/reference/lexical_analysis.html#string-literals

like image 104
nhahtdh Avatar answered Oct 17 '22 22:10

nhahtdh


They're equal because \a means the ASCII Bell character in Python. Looking at the ASCII table, the value of that character is 7.

like image 37
Smi Avatar answered Oct 18 '22 00:10

Smi


It turns out \a and \7 have the same value:

>>> a = '\a'
>>> b = '\7'
>>> a
'\x07'
>>> b
'\x07'

\a is the ASCII Bell (BEL) character (source) which indeed has value 7 in the ASCII table (ASCII table).

like image 2
Simeon Visser Avatar answered Oct 17 '22 23:10

Simeon Visser