Print string as ASCII art in terminal using python

In this article, we shall see how to print the string as ASCII art in terminal using python. This can be done with the help of a python package called pyfiglet. With the help of this package, we can print any string as ASCII character in the terminal with just two lines of code.

Let us install the package to get started.

Installation

This package can be easily installed from pypi.org using pip as

pip install pyfiglet

once the installation is complete, we can proceed to play with this python library. This library does not have any real world applications.

How does it work?

Import the module. Call the print_figlet() method from the pyfiglet library. Just give the text you want to be printed on the terminal to this method and we are good to go. Let us look at it with an example.

import pyfiglet

ascii_art = pyfiglet.print_figlet('Python Coders')
print(ascii_art)

This will print something like this in our terminal.

This library also supports multiple colors. The following colors are supported by this module.

BLACK
RED
GREEN
YELLOW
BLUE
MAGENTA
CYAN
LIGHT_GRAY
DARK_GRAY
LIGHT_RED
LIGHT_GREEN
LIGHT_YELLOW
LIGHT_BLUE
LIGHT_MAGENTA
LIGHT_CYAN
WHITE

All we have to do is provide an extra argument to the print_figlet(colors=color_name) method. This will print our ASCII text in different colors to the terminal.

The following code will print the string in CYAN color to the terminal.

import pyfiglet

ascii_art = pyfiglet.print_figlet('Python Coders', colors='CYAN')
print(ascii_art)

The output of the above code is,

We can try this with one more color. This will also support punctuations. So let us combine them both into a single example.

import pyfiglet

ascii_art = pyfiglet.print_figlet('. , ; ? - = @ % &', colors='LIGHT_RED')
print(ascii_art)

The output of the above image is,

Conclusion

Hope this article is helpful. If you have any issues regarding the installation or running the code, leave your queries in the comment box. I will try to answer them as soon as possible.

Happy coding!

Leave a Reply

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