Saturday, November 16, 2024
Google search engine
HomeLanguagesPrint with your own font using Python !!

Print with your own font using Python !!

Programming’s core function is printing text, but have you ever wished to give it a unique look by utilizing your own custom fonts? Python enables you to use imagination and overcome the standard fonts in your text outputs. In this article, we will do some cool Python tricks. For the user input, the given code will print in the designed font.

Example: 

Input: ROOT
Output:
..######..
..#....#..
..#.##...                    
..#...#...
..#....#..
..######..
..#....#..
..#....#..                    
..#....#..
..######..
..######..
..#....#..
..#....#..                    
..#....#..
..######..
..######..
....##....
....##....                    
....##....
....##....

Explanation: In this, we are printing our own font using Python.

Print with your own font using Python  

Here we are printing our own font using Python. Below is Python3 code implementation :

Python3




# Python3 code to print input in your own font
name = "GEEK"
 
# To take input from User
# name = input("Enter your name: \n\n")
 
length = len(name)
l = ""
 
for x in range(0, length):
    c = name[x]
    c = c.upper()
     
    if (c == "A"):
        print("..######..\n..#....#..\n..######..", end = " ")
        print("\n..#....#..\n..#....#..\n\n")
         
    elif (c == "B"):
        print("..######..\n..#....#..\n..#####...", end = " ")
        print("\n..#....#..\n..######..\n\n")
         
    elif (c == "C"):
        print("..######..\n..#.......\n..#.......", end = " ")
        print("\n..#.......\n..######..\n\n")
         
    elif (c == "D"):
        print("..#####...\n..#....#..\n..#....#..", end = " ")
        print("\n..#....#..\n..#####...\n\n")
         
    elif (c == "E"):
        print("..######..\n..#.......\n..#####...", end = " ")
        print("\n..#.......\n..######..\n\n")
         
    elif (c == "F"):
        print("..######..\n..#.......\n..#####...", end = " ")
        print("\n..#.......\n..#.......\n\n")
         
    elif (c == "G"):
        print("..######..\n..#.......\n..#.####..", end = " ")
        print("\n..#....#..\n..#####...\n\n")
         
    elif (c == "H"):
        print("..#....#..\n..#....#..\n..######..", end = " ")
        print("\n..#....#..\n..#....#..\n\n")
         
    elif (c == "I"):
        print("..######..\n....##....\n....##....", end = " ")
        print("\n....##....\n..######..\n\n")
         
    elif (c == "J"):
        print("..######..\n....##....\n....##....", end = " ")
        print("\n..#.##....\n..####....\n\n")
         
    elif (c == "K"):
        print("..#...#...\n..#..#....\n..##......", end = " ")
        print("\n..#..#....\n..#...#...\n\n")
         
    elif (c == "L"):
        print("..#.......\n..#.......\n..#.......", end = " ")
        print("\n..#.......\n..######..\n\n")
         
    elif (c == "M"):
        print("..#....#..\n..##..##..\n..#.##.#..", end = " ")
        print("\n..#....#..\n..#....#..\n\n")
         
    elif (c == "N"):
        print("..#....#..\n..##...#..\n..#.#..#..", end = " ")
        print("\n..#..#.#..\n..#...##..\n\n")
         
    elif (c == "O"):
        print("..######..\n..#....#..\n..#....#..", end = " ")
        print("\n..#....#..\n..######..\n\n")
         
    elif (c == "P"):
        print("..######..\n..#....#..\n..######..", end = " ")
        print("\n..#.......\n..#.......\n\n")
         
    elif (c == "Q"):
        print("..######..\n..#....#..\n..#.#..#..", end = " ")
        print("\n..#..#.#..\n..######..\n\n")
         
    elif (c == "R"):
        print("..######..\n..#....#..\n..#.##...", end = " ")
        print("\n..#...#...\n..#....#..\n\n")
         
    elif (c == "S"):
        print("..######..\n..#.......\n..######..", end = " ")
        print("\n.......#..\n..######..\n\n")
         
    elif (c == "T"):
        print("..######..\n....##....\n....##....", end = " ")
        print("\n....##....\n....##....\n\n")
         
    elif (c == "U"):
        print("..#....#..\n..#....#..\n..#....#..", end = " ")
        print("\n..#....#..\n..######..\n\n")
         
    elif (c == "V"):
        print("..#....#..\n..#....#..\n..#....#..", end = " ")
        print("\n...#..#...\n....##....\n\n")
         
    elif (c == "W"):
        print("..#....#..\n..#....#..\n..#.##.#..", end = " ")
        print("\n..##..##..\n..#....#..\n\n")
         
    elif (c == "X"):
        print("..#....#..\n...#..#...\n....##....", end = " ")
        print("\n...#..#...\n..#....#..\n\n")
         
    elif (c == "Y"):
        print("..#....#..\n...#..#...\n....##....", end = " ")
        print("\n....##....\n....##....\n\n")
         
    elif (c == "Z"):
        print("..######..\n......#...\n.....#....", end = " ")
        print("\n....#.....\n..######..\n\n")
         
    elif (c == " "):
        print("..........\n..........\n..........", end = " ")
        print("\n..........\n\n")
         
    elif (c == "."):
        print("----..----\n\n")


Output: 

..######..
..#.......
..#.####..
..#....#..
..#####...
..######..
..#.......
..#####...
..#.......
..######..
..######..
..#.......
..#####...
..#.......
..######..
..#...#...
..#..#....
..##......
..#..#....
..#...#...

Time complexity of this code is O(n^2) where n is the length of the input string name. 

The auxiliary space complexity of this code is O(1).

Print with your own font using Python using Dictionary Mapping

Here we will print text using a custom font created using ASCII art representations. This can be done by using a dictionary mapping to associate each uppercase character with its corresponding ASCII art representation.

Python3




ascii_art = {
    "A": "..######..\n..#....#..\n..######..",
    "B": "..######..\n..#....#..\n..#####...",
    "C": "..######..\n..#.......\n..#.......",
    "D": "..######..\n..#.......\n..#.......",
    "E": "..#####...\n..#....#..\n..#....#..",
    "F": "..######..\n..#.......\n..#####...",
    "G": "..######..\n..#.......\n..#.####..",
    "H": "..#....#..\n..#....#..\n..######..",
   
    # Add more characters...
}
 
# Input name for which you want to print ASCII art
name = "GEEK"
 
# Iterate through each character in the name
for char in name:
    # Convert the character to uppercase
    char_upper = char.upper()
     
    # Check if the uppercase character exists in the ASCII art dictionary
    if char_upper in ascii_art:
        # Print the ASCII art representation of the character
        print(ascii_art[char_upper], end=" ")
    else:
        # If the character is not found in the dictionary, print a message
        print("Character not found in ASCII art dictionary")


Output:

..######..
..#.......
..#.####.. ..#####...
..#....#..
..#....#.. ..#####...
..#....#..
..#....#.. Character not found in ASCII art dictionary

Print with your own font using Python using Functions

Here we will print text using a custom font created using ASCII art representations. This can be done by using Functions.

Python3




def print_char(char):
    char_upper = char.upper()
    if char_upper == "A":
        print("..######..\n..#....#..\n..######..", end=" ")
          
    elif char_upper == "B":
        print("..######..\n..#....#..\n..#####...", end=" ")
         
    elif char_upper == "C":
        print("..######..\n..#.......\n..#.......", end = " ")
         
         
    elif char_upper == "D":
        print("..#####...\n..#....#..\n..#....#..", end = " ")
         
         
    elif char_upper == "E":
        print("..######..\n..#.......\n..#####...", end = " ")
        
         
    elif char_upper == "F":
        print("..######..\n..#.......\n..#####...", end = " ")
         
    elif char_upper == "G":
        print("..######..\n..#.......\n..#.####..", end = " ")
         
         
    elif char_upper == "H":
        print("..#....#..\n..#....#..\n..######..", end = " ")
         
    # Add more characters...
 
name = "GEEK"
 
for char in name:
    print_char(char)
    print()  # Print a newline after each character's ASCII art


Output:

..######..
..#.......
..#.####.. 
..######..
..#.......
..#####... 
..######..
..#.......
..#####... 

RELATED ARTICLES

Most Popular

Recent Comments