dimanche 31 janvier 2021

Print a string in python like how dot-matrix printer works

I wanted to print any English alphabet(s) using '*' or any other given special character like how a dot-matrix printer works.

I could come up with a function def printLetters(string, font_size, special_char): which when passed with any letter would print that letter using the special character specified.

Consider the letter 'A':

def printLetters('A', 10, '&'):   # would print the letter A within a 10x10 matrix using '&'

 &&&&&&&&
&        &
&        &
&        &
&&&&&&&&&&
&        &
&        &
&        &
&        &
&        &

and such code snippets for every character. Example for 'A':

FUNCTION_TO_PRINT_A:
    space = ' '
    #print first line
    print('', special_char*(font_size-2))
    for i in range(1, font_size-1):
        #print(i)
        if font_size//2 == i:
            print(special_char*(font_size))
        print(special_char, space*(font_size-4), special_char)

printLetters(10, "&")

But when the parameter string has more than one characters, it prints gibberish after first character.
So I just wanted some ideas/code-snippets which would print the first row of all characters in string first and so on until the last row so that all those characters line up side by side horizontally on the console.

Aucun commentaire:

Enregistrer un commentaire