pythonwindowscmdcolorscolorama

print() outputs ASCII code + character instead of colored text


I took the reference of How to make every character/line print in a random color? and changed the text to ghost ASCII art but the output is not printing the colored art but printing ascii code + the symbols used in the text.

import colorama
import random


    
text = """
     .-----.
   .' -   - '.
  /  .-. .-.  \
  |  | | | |  |
   \ \o/ \o/ /
  _/    ^    \_
 | \  '---'  / |
 / /`--. .--`\ \
/ /'---` `---'\ \
'.__.       .__.'
    `|     |`
     |     \
     \      '--.
      '.        `\
        `'---.   |
              ) /
              \/
      """        
colors = list(vars(colorama.Fore).values())
colored_chars = [random.choice(colors) + char for char in text]
print(''.join(colored_chars))

Output :enter image description here


Solution

  • Try calling os.system('cls') before printing to the console with colors.

    Also include r"" before your string to format it correctly (worked for me).

    import colorama
    import random
    import os
    
    
    
    text = r"""
         .-----.
       .' -   - '.
      /  .-. .-.  \
      |  | | | |  |
       \ \o/ \o/ /
      _/    ^    \_
     | \  '---'  / |
     / /`--. .--`\ \
    / /'---` `---'\ \
    '.__.       .__.'
        `|     |`
         |     \
         \      '--.
          '.        `\
            `'---.   |
                  ) /
                  \/
          """
    
    os.system("cls")
    colors = list(vars(colorama.Fore).values())
    colored_chars = [random.choice(colors) + char for char in text]
    print(''.join(colored_chars))