What is the use for control characters in string.printable?

I was learning about the format mini-language and I scrolled up to view some string things, and I wondered what Python considered printable, so I checked:

>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

Note at the end, along with other normal printable control-type characters like ^I, ^J, and ^M, there's also ^B and ^C.

What would be the use for these?

Asked by: Lenny385 | Posted: 06-12-2021

Answer 1

\x0b is the U+000B, LINE TABULATION, and \x0c is U+000C, FORM FEED (FF).

Great. What does that mean, and why are they considered printable?

Back in the days of teletypes, ASCII provided these characters for advancing a sheet of paper through the printer. The line tabulation character was to a line feed like a horizontal tab was to a space. The printer could have a set of defined vertical tab stops, and a line tabulation character would be interpreted as a request to advance to the next one following the current line.

A form feed would advance to the top of the next page, however the device defined a page. (If it used a continuous paper feed, a "page" would be considered, say, 66 lines, and if you were currently on line 60, a form feed would simply advance 7 lines, to line 1 of the next page.)

On a modern terminal emulator, they tend not to have any particular meaning. Simple tests indicate that on an xterm, they both appear to be treated the same as a line feed followed by a space:

>>> print("x\x0cy")
>>> print("x\x0by")

Update: after seeing https://stackoverflow.com/a/58421132/1126841, it would appear isn't not just a space following the line feed, but rather a line feed not followed by a carriage return, i.e., the cursor advances one line without returning to the beginning of that line; compare with print("\n"), which advances to the beginning of the next line, regardless of where the cursor currently is. (That makes more sense, and I should have remembered that.)

Answered by: Daryl954 | Posted: 07-01-2022

Answer 2

These are types of whitespace characters:

>>> string.whitespace
' \t\n\r\x0b\x0c'

From the docs:

A string containing all ASCII characters that are considered whitespace. This includes the characters space, tab, linefeed, return, formfeed, and vertical tab.

We know this by following the docs for printable:

String of ASCII characters which are considered printable. This is a combination of digits, ascii_letters, punctuation, and whitespace.

And we know what these whitespace characters are by referencing unicode control characters:
\x0b is a vertical tab
\x0c is a form feed

Answered by: Roman821 | Posted: 07-01-2022

Answer 3

Those symbols are printable whitespaces (can also be obtained by string.whitespace). \x0b or \v is vertical tab and \x0c (\f) which is form feed (docs).

In various terminals the representation can be different, but usually \v looks like following:

>>> print("some\vtext\vhere")

\f forces the printer to eject the current page and to continue printing at the top of another. In terminal often displays as an empty line. Sometimes can be used to clear the screen.

That's why these symbols are considered as printable. However, representation and behavior may be quite different

Answered by: Melissa791 | Posted: 07-01-2022

Answer 4

From the docs for string.printable

String of characters which are considered printable. This is a combination of digits, letters, punctuation, and whitespace.

where whitespace is defined as

A string containing all characters that are considered whitespace. On most systems this includes the characters space, tab, linefeed, return, formfeed, and vertical tab.

These are whitespace characters.

Answered by: Samantha235 | Posted: 07-01-2022

Answer 5

Those characters belong to the family of whitespace characters (you can check >>> string.whitespace and python documentation for it). Specifically, \x0b is Unicode U+B for "Line Tabulation" and the \x0c is Unicode U+C for "Form Feed".

Answered by: Anna292 | Posted: 07-01-2022

Similar questions

Why does Python's string.printable contains unprintable characters?

I have two String.printable mysteries in the one question. First, in Python 2.6: &gt;&gt;&gt; string.printable '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&amp;\'()*+,-./:;&lt;=&gt;?@[\\]^_`{|}~ \t\n\r\x0b\x0c' Look at the end of the string, and you'll find '\x0b\x0c' sticking out like a sore-thumb. Why are they there? I am using a machine set to Australian ...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python