Search code examples
pythonasciifile-writingnon-printable

Characters that are not visible when printed


I was using an ascii code table the other day and noticed that some "unprintable" characters exist. These characters include "Null", "Start of Header", and others like them. I tested these characters myself by attempting to print "Null" to the console like this:

print ("Hello" + chr(0))

The word Hello was output as well as a little white box with the letters "NUL". I was expecting there to be no output at all for the Null character - I thought the output would simply be "Hello".

Is this output unique to Python? If the output is written to a file is no character written? Also, if "Hello + chr(0)" is written somewhere and then read in by another program, will the program recognize the "Null" or will it be read as if nothing is there? Basically, is anything hidden, does anything disappear, and what is visible?

Thank you!


Solution

  • This is a function of your console display settings. Python simply sends the given ASCII codes to your console, which it sees as Just Another Output Device, pretty much the same as writing to a file. How to display each character is up to the console's input manager. For instance, note how some applications will translate :-) into a single-character smiley face icon? That's done in the display application on the other end.

    You could demonstrate this by writing an equivalent output program in another language -- see how it handles the NUL character the same way.

    My personal favourite is a timed string of ASCII 007 characters. That sounds the terminal bell (usually a small "beep" these days).