pythonstringletter-spacing

My python prgram print the enlarged word vertically instead of horizontally


When a user enters a word, my Python program enlarges it with asterisks.But, when the user enters a word, it prints vertically instead of horizontally.

My question is - How to pring the enlarged letter horizantally (SIDE BY SIDE) instead of vertically?

Here is my code -

def a():
    row1 = [3]
    row2 = [2, 4]
    row3 = [1, 2, 3, 4, 5]
    row4 = [1, 5]
    row5 = [1, 5]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def b():
    row1 = [1, 2, 3, 4]
    row2 = [1, 5]
    row3 = [1, 2, 3, 4]
    row4 = [1, 5]
    row5 = [1, 2, 3, 4]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def c():
    row1 = [2, 3, 4, 5]
    row2 = [1]
    row3 = [1]
    row4 = [1]
    row5 = [2, 3, 4, 5]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def d():
    row1 = [1, 2, 3, 4]
    row2 = [1, 5]
    row3 = [1, 5]
    row4 = [1, 5]
    row5 = [1, 2, 3, 4]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def e():
    row1 = [1, 2, 3, 4, 5]
    row2 = [1]
    row3 = [1, 2, 3, 4, 5]
    row4 = [1]
    row5 = [1, 2, 3, 4, 5]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def f():
    row1 = [1, 2, 3, 4, 5]
    row2 = [1]
    row3 = [1, 2, 3, 4, 5]
    row4 = [1]
    row5 = [1]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def g():
    row1 = [2, 3, 4, 5]
    row2 = [1]
    row3 = [1, 3, 4, 5]
    row4 = [1, 5]
    row5 = [2, 3, 4, 5]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def h():
    row1 = [1, 5]
    row2 = [1, 5]
    row3 = [1, 2, 3, 4, 5]
    row4 = [1, 5]
    row5 = [1, 5]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def i():
    row1 = [1, 2, 3, 4, 5]
    row2 = [3]
    row3 = [3]
    row4 = [3]
    row5 = [1, 2, 3, 4, 5]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def j():
    row1 = [1, 2, 3, 4, 5]
    row2 = [3]
    row3 = [3]
    row4 = [1, 3]
    row5 = [2, 3]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def k():
    row1 = [1, 4]
    row2 = [1, 3]
    row3 = [1, 2]
    row4 = [1, 3]
    row5 = [1, 4]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def l():
    row1 = [1]
    row2 = [1]
    row3 = [1]
    row4 = [1]
    row5 = [1, 2, 3]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def m():
    row1 = [1, 5]
    row2 = [1, 2, 4, 5]
    row3 = [1, 3, 5]
    row4 = [1, 5]
    row5 = [1, 5]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def n():
    row1 = [1, 5]
    row2 = [1, 2, 5]
    row3 = [1, 3, 5]
    row4 = [1, 4, 5]
    row5 = [1, 5]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def o():
    row1 = [2, 3, 4]
    row2 = [1, 5]
    row3 = [1, 5]
    row4 = [1, 5]
    row5 = [2, 3, 4]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def p():
    row1 = [1, 2, 3, 4]
    row2 = [1, 5]
    row3 = [1, 2, 3, 4]
    row4 = [1]
    row5 = [1]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def q():
    row1 = [3]
    row2 = [2, 4]
    row3 = [1, 3, 5]
    row4 = [2, 4]
    row5 = [3, 5]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def r():
    row1 = [1, 2, 3, 4]
    row2 = [1, 5]
    row3 = [1, 2, 3, 4]
    row4 = [1, 3]
    row5 = [1, 4, 5]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def s():
    row1 = [2, 3, 4, 5]
    row2 = [1]
    row3 = [2, 3, 4]
    row4 = [5]
    row5 = [1, 2, 3, 4]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def t():
    row1 = [1, 2, 3, 4, 5]
    row2 = [3]
    row3 = [3]
    row4 = [3]
    row5 = [3]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def u():
    row1 = [1, 5]
    row2 = [1, 5]
    row3 = [1, 5]
    row4 = [1, 5]
    row5 = [2, 3, 4]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def v():
    row1 = [1, 5]
    row2 = [1, 5]
    row3 = [1, 5]
    row4 = [2, 4]
    row5 = [3]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def w():
    row1 = [1, 5]
    row2 = [1, 5]
    row3 = [1, 3, 5]
    row4 = [1, 2, 4, 5]
    row5 = [1, 5]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def x():
    row1 = [1, 5]
    row2 = [2, 4]
    row3 = [3]
    row4 = [2, 4]
    row5 = [1, 5]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def y():
    row1 = [1, 5]
    row2 = [2, 4]
    row3 = [3]
    row4 = [3]
    row5 = [3]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")


def z():
    row1 = [1, 2, 3, 4, 5]
    row2 = [4]
    row3 = [3]
    row4 = [2]
    row5 = [1, 2, 3, 4, 5]
    coordinates = [row1, row2, row3, row4, row5]
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                print("*", end="")
            else:
                print(" ", end="")
        print("")



word = input('Enter a word - ').lower().strip()

while True:
    if not word.isalpha():
        print("Sorry, but you need to enter a valid word containing only letters!")
        print(" ")
        word = input("Enter a word - ").lower().strip()
    else:
        break

for letter in word:
    if letter == "a":
        a()
    elif letter == "b":
        b()
    elif letter == "c":
        c()
    elif letter == "d":
        d()
    elif letter == "e":
        e()
    elif letter == "f":
        f()
    elif letter == "g":
        g()
    elif letter == "h":
        h()
    elif letter == "i":
        i()
    elif letter == "j":
        j()
    elif letter == "k":
        k()
    elif letter == "l":
        l()
    elif letter == "m":
        m()
    elif letter == "n":
        n()
    elif letter == "o":
        o()
    elif letter == "p":
        p()
    elif letter == "q":
        q()
    elif letter == "r":
        r()
    elif letter == "s":
        s()
    elif letter == "t":
        t()
    elif letter == "u":
        u()
    elif letter == "v":
        v()
    elif letter == "w":
        w()
    elif letter == "x":
        x()
    elif letter == "y":
        y()
    elif letter == "z":
        z()

This program prints the word vertically.

here is an example of how it prints - C:\Users\yaswa\PycharmProjects\pythonProject\venv\Scripts\python.exe "C:\py letters\blah.py"

Enter a word - hi
*   *
*   *
*****
*   *
*   *
*****
  *  
  *  
  *  
*****

Process finished with exit code 0

What I expect - I expect it to print "HI" side by side. Like this -

*   * *****
*   *   *
*****   *
*   *   *
*   * *****

Solution

  • Your code was too redundant, so I rewrote it.

    alphabets = {
        "a" : [
            [3],
            [2, 4],
            [1, 2, 3, 4, 5],
            [1, 5],
            [1, 5]
        ],
        "b" : [
            [1, 2, 3, 4],
            [1, 5],
            [1, 2, 3, 4],
            [1, 5],
            [1, 2, 3, 4]
        ],
        "c" : [
            [2, 3, 4, 5],
            [1],
            [1],
            [1],
            [2, 3, 4, 5]
        ],
        "d" : [
            [1, 2, 3, 4],
            [1, 5],
            [1, 5],
            [1, 5],
            [1, 2, 3, 4]
        ],
        "e" : [
            [1, 2, 3, 4, 5],
            [1],
            [1, 2, 3, 4, 5],
            [1],
            [1, 2, 3, 4, 5]
        ],
        "f" : [
            [1, 2, 3, 4, 5],
            [1],
            [1, 2, 3, 4, 5],
            [1],
            [1]
        ],
        "g" : [
            [2, 3, 4, 5],
            [1],
            [1, 3, 4, 5],
            [1, 5],
            [2, 3, 4, 5]
        ],
        "h" : [
            [1, 5],
            [1, 5],
            [1, 2, 3, 4, 5],
            [1, 5],
            [1, 5]
        ],
        "i" : [
            [1, 2, 3, 4, 5],
            [3],
            [3],
            [3],
            [1, 2, 3, 4, 5]
        ],
        "j" : [
            [1, 2, 3, 4, 5],
            [3],
            [3],
            [1, 3],
            [2, 3]
        ],
        "k" : [
            [1, 4],
            [1, 3],
            [1, 2],
            [1, 3],
            [1, 4]
        ],
        "l" : [
            [1],
            [1],
            [1],
            [1],
            [1, 2, 3]
        ],
        "m" : [
            [1, 5],
            [1, 2, 4, 5],
            [1, 3, 5],
            [1, 5],
            [1, 5]
        ],
        "n" : [
            [1, 5],
            [1, 2, 5],
            [1, 3, 5],
            [1, 4, 5],
            [1, 5]
        ],
        "o" : [
            [2, 3, 4],
            [1, 5],
            [1, 5],
            [1, 5],
            [2, 3, 4]
        ],
        "p" : [
            [1, 2, 3, 4],
            [1, 5],
            [1, 2, 3, 4],
            [1],
            [1]
        ],
        "q" : [
            [3],
            [2, 4],
            [1, 3, 5],
            [2, 4],
            [3, 5]
        ],
        "r" : [
            [1, 2, 3, 4],
            [1, 5],
            [1, 2, 3, 4],
            [1, 3],
            [1, 4, 5]
        ],
        "s" : [
            [2, 3, 4, 5],
            [1],
            [2, 3, 4],
            [5],
            [1, 2, 3, 4]
        ],
        "t" : [
            [1, 2, 3, 4, 5],
            [3],
            [3],
            [3],
            [3]
        ],
        "u" : [
            [1, 5],
            [1, 5],
            [1, 5],
            [1, 5],
            [2, 3, 4]
        ],
        "v" : [
            [1, 5],
            [1, 5],
            [1, 5],
            [2, 4],
            [3]
        ],
        "w" : [
            [1, 5],
            [1, 5],
            [1, 3, 5],
            [1, 2, 4, 5],
            [1, 5]
        ],
        "x" : [
            [1, 5],
            [2, 4],
            [3],
            [2, 4],
            [1, 5]
        ],
        "y" : [
            [1, 5],
            [2, 4],
            [3],
            [3],
            [3]
        ],
        "z" : [
            [1, 2, 3, 4, 5],
            [4],
            [3],
            [2],
            [1, 2, 3, 4, 5]
        ],
    }
    
    
    def add_alphabet(coordinates, output):
        for i in range(5):
            x = coordinates[i]
            for j in range(6):
                if j in x:
                    output[i].append("*")
                else:
                    output[i].append(" ")
            
        return output
    
    
    word = input('Enter a word - ').lower().strip()
    
    while True:
        if not word.isalpha():
            print("Sorry, but you need to enter a valid word containing only letters!")
            print(" ")
            word = input("Enter a word - ").lower().strip()
        else:
            break
    
    output = [[], [], [], [], []]
    
    for letter in word:
        output = add_alphabet(alphabets[letter], output)
    
    for line in output:
        print(''.join(line))
    

    In this code, I am keeping each of the 5 lines in a buffer before print, updating it with values from each alphabet from the input and printing at the end.

    Output:

    Enter a word - Hi
     *   * *****
     *   *   *  
     *****   *  
     *   *   *  
     *   * *****