ASCII ART

ASCII character encoding is essential knowledge for Computer Science. It is basically a set of values which represent text characters, enabling the transmission of messages via electronic media.

A great way to get motivated to learn about ASCII is ASCII art. Some of you may have seen ASCII art before – it’s graphics made up from ASCII characters, and was very popular in the early days of home computing when the graphics capabilities of the available machines were quite limited. There are some great examples on this website.

You can find a great tool to convert text into ASCII art in lots of different styles here. To make ASCII art from images, try this website.

PYTHON AND THE ASCII CHARACTER SET

In terms of Python programming for Computer Science, the most useful functions for learning about and working with ASCII are ord() and chr().

for example, run the following code in you chosen python coding environment

print(ord('A')) # prints the ASCII code for the character 'A'

Try a few other characters of your own.

Now something a bit more adventurous:

for char in 'Hello world':
    print(ord(char)) # Print the ASCII value for each character in turn

For the other direction, ASCII to text, we use chr(). E.g:

print(chr(65)) # Print character for ASCII code 65

As an exercise, use an ASCII table to predict what these numbers will produce:

80 121 116 104 111 110

CHARACTER SETS COMPUTER SCIENCE LESSON

If you want to master character sets for Computer Science, check out this complete lesson with presentation, ASCII table, practice activities and Python code.

PRINTING ASCII ART IN A PYTHON PROGRAM

Check out this ASCII art:

 /\_/\
( o.o )
 > ^ <


 _._     _,-'""`-._
(,-.`._,'(       |\`-/|
    `-.-' \ )-`( , o o)
          `-    \`_`"'-


    |\__/,|   (`\
  _.|o o  |_   ) )
-(((---(((--------



  _____       _   _
 |  __ \     | | | |
 | |__) |   _| |_| |__   ___  _ __
 |  ___/ | | | __| '_ \ / _ \| '_ \
 | |   | |_| | |_| | | | (_) | | | |
 |_|    \__, |\__|_| |_|\___/|_| |_|
         __/ |
        |___/

Pretty cool huh?

The trick to displaying these kinds of examples in Python is the use of two bits of syntax that not everyone knows about. These are the triple quotation marks (""" """) and the r character placed before them. A template you can use is:

print(r"""

""")

The triple quotation marks tell Python that your string is multi-line, and the r character means print the string as raw, meaning that all characters will be printed literally and any special character such backslashes which are sometimes used as escape characters will be treated as plain text.

The code which produces the above output is shown below:

print(r"""
 /\_/\
( o.o )
 > ^ <
""")


print(r"""
 _._     _,-'""`-._
(,-.`._,'(       |\`-/|
    `-.-' \ )-`( , o o)
          `-    \`_`"'-
""")

print(r"""
    |\__/,|   (`\
  _.|o o  |_   ) )
-(((---(((--------
""")

print(r"""

  _____       _   _                 
 |  __ \     | | | |                
 | |__) |   _| |_| |__   ___  _ __  
 |  ___/ | | | __| '_ \ / _ \| '_ \ 
 | |   | |_| | |_| | | | (_) | | | |
 |_|    \__, |\__|_| |_|\___/|_| |_|
         __/ |                      
        |___/                       

""")

To generate the ASCII are text used above, which is great to use when writing console programs to make them look much more interesting, copy and paste the ascii art text text from an online tool such as this one.

CHARACTER SETS SLIDE SHOW

If you want to master character sets for GCSE Computer Science, check out this complete lesson with presentation, ASCII table, practice activities and Python code.


Hopefully you now have a greater understanding of ASCII character encoding and maybe had some fun on the way!

Sharing is caring!

1 Comment on “ASCII ART

Leave a Reply

Your email address will not be published. Required fields are marked *