Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have two String.printable mysteries in the one question.

First, in Python 2.6:

>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \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 settings, so there shouldn't be any accented characters or the like.

Next, try running this code:

for x in string.printable: print x,
print
for x in string.printable: print x

The first line successfully prints all the characters separated by a space. The two odd characters turn out as the Male and Female symbols.

The second line successfully prints all the characters EXCEPT THE LAST separated by a line feed. The Male symbol prints; the female symbol is replaced with a missing character (a box).

I'm sure Python wasn't intended to be gender-biased, so what gives with the difference?

like image 355
Oddthinking Avatar asked Jan 06 '09 21:01

Oddthinking


People also ask

How can I check all characters in a string is printable?

The isprintable() method returns “True” if all characters in the string are printable or the string is empty, Otherwise, It returns “False”. This function is used to check if the argument contains any printable characters such as: Digits ( 0123456789 ) Uppercase letters ( ABCDEFGHIJKLMNOPQRSTUVWXYZ )

How do I remove non printable characters from a string in Python?

To strip non printable characters from a string in Python, we can call the isprintable method on each character and use list comprehension. to check if each character in my_string is printable with isprintable . Then we call ''. join with the iterator to join the printable characters in my_string back to a string.

What is a printable string in Python?

printable is a pre-initialized string used as string constant. In Python, string. printable will give the all sets of punctuation, digits, ascii_letters and whitespace. Syntax : string.printable.

How do you delete a hidden character in Python?

translate(table). trim()) which ignores the unwanted characters at the beginning and the end of the string. Or you use len("my string". translate(table, unwanted_chars)) which will ignore all you unwanted characters.


1 Answers

There is a difference in "printable" for "can be displayed on your screen". Your terminal displays the low ascii printer control codes 0x0B and 0x0C as the male and female symbols because that is what those indices in your font contain. Those characters are more accurately described as the Vertical Tabulator and Form Feed characters. These two characters, along with \t \r and \n, are all printable, and do well defined things on a printer.

like image 92
Sparr Avatar answered Oct 25 '22 20:10

Sparr