ASCII Art Generator with Font Selection

  • Share this:

Code introduction


This function generates ASCII art from a given text by selecting a font and applying font lines to each character.


Technology Stack : Built-in libraries: string, itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def ascii_art(text, font_size=10):
    """
    Generate ASCII art from a given text using a specified font size.
    """
    import string
    from itertools import cycle

    # ASCII art fonts
    fonts = {
        10: [
            "  #   #  ",
            " ### ### ",
            "##### #####",
            " ### ### ",
            "  #   #  "
        ],
        20: [
            "    #    ",
            "   ###   ",
            "  #####  ",
            "   ###   ",
            "    #    "
        ]
    }

    # Select the font based on size
    font = fonts.get(font_size, fonts[10])

    # Create the ASCII art
    ascii_art = ""
    for line in text:
        ascii_art += line.upper() + "\n"
        for font_line in font:
            ascii_art += "".join([font_line if c in string.ascii_letters else " " for c in line.upper()]) + "\n"

    return ascii_art