Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python prints ascii code + character instead of colored text

I took the reference of Python – Make every character/line random color print? 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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading