37

Typing string.whitespace gives you a string containing all whitespace characters defined by Python's string module:

'\t\n\x0b\x0c\r '

Both \x0b and \x0c seem to give a vertical tab.

>>> print 'first\x0bsecond'
first
     second

\v gives the same effect. How are these three different? Why does the string module use \x0b or \x0c over the simpler \v?

wwii
  • 23,232
  • 7
  • 37
  • 77
LLaP
  • 2,528
  • 4
  • 22
  • 34

1 Answers1

57

\v is \x0b:

>>> '\v'
'\x0b'

but the string literal representation in Python is using the \x0b notation instead.

The Python string literal representation only ever uses \n, \r and \t, everything else that is not a printable ASCII character is represented using the \xhh notation instead.

\x0c is a form feed; it forces a printer to move to the next sheet of paper. You can also express it as \f in Python:

>>> '\f'
'\x0c'

In terminals the effects of \v and \f are often the same.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • doesn't work for me (Python 2.7.11, OSx). Any of those variations prints out a new line too.. Whitespace is \x20 – magicrebirth Feb 18 '16 at 11:27
  • @magicrebirth: sorry, what doesn't work? The variations I demonstrate are *string literals*, echoed by the interactive interpreter. And Python will *never* echo `\x20` for a space. – Martijn Pieters Feb 18 '16 at 11:33
  • 4
    Nice. I ran across `\x0c` when converting OCR'd pdfs into text. The blank pages of the pdfs were returned to me as `\x0c`, which now makes perfect sense. – Monica Heddneck May 25 '17 at 03:08