0

I have tested out some escape sequences. Most of them work, but some of them do not. Why is this?

This is the code I wrote for testing the escape sequences:

# -*- coding: UTF-8 -*-

tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split \non a line."
backslash_cat = "I'm \\ a \\ cat."

fat_cat = '''
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
'''

print tabby_cat
print persian_cat
print backslash_cat
print fat_cat

#while True:
#   for i in  ["/", "-", "|", "\\", "|"]:
#       print "%s\r" % i,

print "This should ring a bell \a"
print "Does this have a backspace\b"    
print "Printing \"double quotes\" and some \'single quotes\'"    
print "Formfeed.\fWill this work?"    
print "Linefeed.\nWill this work?"    
print "Print the character named name: \N{name}"    
print "Print a carriage return.\rWill this work?"    
print "A 16 bit unicode character: \u6C34"    
print "Let's check this vertical tab: \v\vWhat is this?"
print "This should output an octal value: \111"
print "This should output a hex value: \xAA"

And the output in the terminal:

    I'm tabbed in.
I'm split 
on a line.
I'm \ a \ cat.

I'll do a list:
    * Cat food
    * Fishies
    * Catnip
    * Grass

This should ring a bell 
Does this have a backspace
Printing "double quotes" and some 'single quotes'
Formfeed.
         Will this work?
Linefeed.
Will this work?
Print the character named name: \N{name}
Will this work?e return.
A 16 bit unicode character: \u6C34
Let's check this vertical tab: 

                               What is this?
This should output an octal value: I
This should output a hex value: �

The ones that do not work are:

  • Bell: \a
  • Backspace: \b
  • Character name: \N{name} (Solved)
  • 16 and 32 bit unicode characters: \u6C34 (Solved)

What should I do to make these escape sequences work?

================EDIT=====================

print u"Print the character named name: \N{BLACK SPADE SUIT}"
print u"A 16 bit unicode character: \u6C34"

Print the character named name: ♠
A 16 bit unicode character: 水
  • 16 and 32 bit unicode characters: \u6C34
  • Character name: \N{name} These works just fine when preceding the string with a u
Johan Vergeer
  • 5,208
  • 10
  • 48
  • 105

1 Answers1

2

The \a and \b sequences work just fine, but your terminal may ignore them at will. The sequences are just alias for creating a byte with hex values 07 and 08, respectively:

>>> '\a'
'\x07'
>>> '\b'
'\x08'

Both sequences result in one byte each. It is up to the receiving terminal to interpret the byte as a control charater, the terminal is free to ignore the byte or do something different with it.

Some terminals will flash instead of sound a bell; this is usually configurable. On my terminal \b works just fine to move the cursor; it doesn't erase the preceding character. The change in position becomes visible when you add more text after it:

>>> print 'abc\b'
abc
>>> print 'abc\bd'
abd

The d overwrote the c character because the backspace character moved the cursor position back one position.

Unicode escape sequences only work in Unicode strings, using u'...' literals:

>>> print "A 16 bit unicode character: \u6C34"
A 16 bit unicode character: \u6C34
>>> print u"A 16 bit unicode character: \u6C34"
A 16 bit unicode character: 水
>>> print u"Print the character named: \N{CJK UNIFIED IDEOGRAPH-6C34}"
Print the character named: 水

Note that it also depends on your terminal (console) if it actually supports the Unicode codepoints printed!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks @MartijnPieters. I already figured out the unicode characters myself, but your explanation is a lot better. Also for the \a and \b escape sequences I get it now. – Johan Vergeer Nov 06 '15 at 19:22