Search code examples
pythonpython-3.xmultilinemultilinestring

Print multiline text horizontally


In my python program I defined a dictionary.

And assigned big block letters made with # symbols to it.

I need to display letters horizontally like this 👇

  #    ###     ###
 # #   #  #   #
#####  ###   #
#   #  #  #   #
#   #  ###     ###

My Code is supposed to take an input and print the big letters corresponding the input if input is abc so output should be like above.

Code 👇

dic ={}

dic['A'] = '''
  #  
 # # 
#####
#   #
#   #'''
dic['B'] = '''
###  
#  # 
###  
#  # 
###  '''
dic['C'] = '''
  ### 
 #   
#    
 #   
  ###'''

word = input('Input : ').upper()

for i in word :
    s = dic[i].split('\n')
    print(s[0],end='     ')
print('')
for j in word :
    print(s[1],end='     ')
print('')
for k in word :
    print(s[2],end='     ')
print('')
for m in word :
    print(s[3],end='     ')
print('')
for n in word :
    print(s[4],end='     ')

Solution

  • Store the chars in lists like this:

    chars = {}
    chars['A'] = ['  #  ',
                  ' # # ',
                  '#####',
                  '#   #',
                  '#   #']
    
    chars['B'] = ['### ',
                  '#  #',
                  '### ',
                  '#  #',
                  '### ']
    
    chars['C'] = ['  ###',
                  ' #   ',
                  '#    ',
                  ' #   ',
                  '  ###']
    
    word = input('Input : ').upper()
    word_chars = [chars[i] for i in word]
    

    Then use this function to combine the chars in word_chars:

    def combineChars(chars: list[list[str]], spacing: str):
        lines = []
        for line_i in range(max([len(x) for x in chars])):
            line = []
            for char in chars:
                if len(char) > line_i:
                    line.append(char[line_i])
            lines.append(spacing.join(line))
        return '\n'.join(lines)
    
    
    print(combineChars(word_chars, '  '))  # in this case the chars
                                           # are 2 spaces apart 
    #output:
    
      #    ###     ###
     # #   #  #   #
    #####  ###   #
    #   #  #  #   #
    #   #  ###     ###
    

    The second argument in combineChars(chars, spacing) is used for the spacing inbetween the chars.

    There is also a short, but complicated form:

    def combineChars(chars: list[list], spacing: str):
        return '\n'.join([spacing.join([char[line_i] for char in chars if len(char) > line_i]) for line_i in range(max([len(x) for x in chars]))])