DEV Community

Diamantino Almeida
Diamantino Almeida

Posted on

Creating ASCII Art with Python: A Fun Guide to Text Transformations

ASCII art has long been a charming way to bring text to life, giving it style and personality with creative letter formations and symbols.

If you’ve ever admired intricate designs created purely with characters, you’ll be happy to know you can create your own ASCII art easily with Python! In this post, we'll explore how to transform any text into ASCII art using a simple Python library called pyfiglet.

Why ASCII Art?

ASCII art is unique in its appeal—it brings a nostalgic, retro vibe to your text. It’s widely used to add flair to terminal outputs, messages, or even for aesthetic value in web and software design.

Whether it’s creating a header for a terminal-based game or simply adding some creative fun to your Python project, ASCII art can be a rewarding addition.

Getting Started with pyfiglet

The pyfiglet library in Python makes it simple to convert text into various ASCII art styles. Originally inspired by the classic Figlet tool (hence the name, Python Figlet), pyfiglet offers a wide range of fonts and allows you to generate impressive text art in just a few lines.

Step 1: Install pyfiglet

Before starting, make sure to install pyfiglet. You can do this via pip:

pip install pyfiglet
Enter fullscreen mode Exit fullscreen mode

Step 2: Writing the Script

import pyfiglet

# Text you want to convert to ASCII art
text = "Hello, World!"

# Convert text using figlet format
ascii_art = pyfiglet.figlet_format(text)
print(ascii_art)
Enter fullscreen mode Exit fullscreen mode

This code will output "Hello, World!" in the default ASCII art style. The magic lies in figlet_format(), which takes any string and applies an ASCII font style to it.

Experimenting with Fonts

One of the exciting features of pyfiglet is its font options. There are dozens of fonts to choose from, each giving a unique style. For instance, try using fonts like "slant," "3-d," "banner," or "big" by modifying the

figlet_format() function:

ascii_art = pyfiglet.figlet_format(text, font="slant")
print(ascii_art)
Enter fullscreen mode Exit fullscreen mode

This changes the font to “slant,” giving the text a diagonal appearance. To find the full list of available fonts, you can run this code:

import pyfiglet

fonts = pyfiglet.FigletFont.getFonts()
print(fonts)
Enter fullscreen mode Exit fullscreen mode

User Input for Customization

To make your ASCII generator interactive, you can allow users to input the text and font of their choice:

text = input("Enter the text you want to convert: ")
font = input("Choose a font (or press Enter for default): ")

# Use the user-selected font or default to standard
ascii_art = pyfiglet.figlet_format(text, font=font if font else "standard")
print(ascii_art)
Enter fullscreen mode Exit fullscreen mode

Now users can create ASCII art in real-time, selecting the style that best fits their needs.

Adding More Flair

You can expand this script to make it even more engaging. For instance, consider combining it with color libraries like termcolor to add vibrant colors to your ASCII art output in the terminal.

Install termcolor via pip:

pip install termcolor

Enter fullscreen mode Exit fullscreen mode

Then, apply colors to your ASCII art output:

from termcolor import colored

ascii_art = pyfiglet.figlet_format(text, font=font if font else "standard")
print(colored(ascii_art, 'cyan'))
Enter fullscreen mode Exit fullscreen mode

Final Thoughts

Generating ASCII art in Python is an enjoyable project that adds creativity and personality to your programming.

Whether you use it to beautify a terminal output or as a signature at the end of a script, ASCII art can be a playful touch that delights both you and your users.

With just a few lines of code, pyfiglet makes it easy to transform text into artwork. Experiment with fonts, colors, and different phrases—and have fun watching your words come to life in ASCII!

Top comments (0)