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

904 Views Asked by At

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-2), special_char, sep = '')

printLetters('A', 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.

1

There are 1 best solutions below

4
On

Ah, fond memories. We used to do this sort of thing all the time in the olden days before GUIs. It is good that you want to define the shape of each letter separately, but making a separate function to print each letter is obviously not useful. After you print the N lines of one letter, you have no way to get back to the top.

You know that you need to print the top line of ALL letters, then the 2nd line of ALL letters, etc. I don't want to write you a complete example, because it's pretty important for you to learn how to figure this kind of thing out, but why don't you start with something like this:

font = {
    'A': [
        ' ###  ',
        '#   # ',
        '##### ',
        '#   # ',
        '#   # '
    ],
    'B': [
        '####  ',
        '#   # ',
        '####  ',
        '#   # ',
        '####  '
    ],
    ...etc...
}