1

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

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 3
    This works fine, *if* your terminal understands ANSI escape codes. The Windows command prompt is not such a terminal. – chepner Mar 04 '22 at 18:38
  • 1
    It looks lke your terminal emulator isn't interpreting the `ESC` character correctly, it's displaying a left-arrow instead. – Barmar Mar 04 '22 at 18:38
  • 1
    I think you need to call `colorama.init()` after importing it. – snakecharmerb Mar 04 '22 at 19:01
  • @chepner: The latest versions of the Window command prompt *do* understand ANSI escape codes. See [How to make win32 console recognize ANSI/VT100 escape sequences?](https://stackoverflow.com/questions/16755142/how-to-make-win32-console-recognize-ansi-vt100-escape-sequences) – martineau Mar 04 '22 at 20:22
  • I should have said "does not appear to be"; I don't use Windows myself. – chepner Mar 04 '22 at 23:04

1 Answers1

1

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))
chepner
  • 497,756
  • 71
  • 530
  • 681
Richard
  • 416
  • 4
  • 13
  • 1
    That shouldn't have any effect on the escape sequences to display colors. – Barmar Mar 04 '22 at 18:39
  • 1
    The point about using a raw string literal is valid, but nothing else here will fix the terminal's inability to understand ANSI escape codes. – chepner Mar 04 '22 at 18:39
  • 1
    It does for me, I have ran into this issue in the past on my own projects and clearing the screen works for some reason. – Richard Mar 04 '22 at 18:39
  • 1
    it works with this . i don't understand why few are saying no effect on escape sequences and terminals inability. Anyways the question is now closed with similar question which actually is not a similar question. Thank you. – AKSHAY ARJUN Mar 04 '22 at 18:54
  • 1
    Some are quick to jump to a conclusion based on their own understanding. In theory, just clearing the screen shouldn't work. But outside of my own understanding it just does and I'm not sure why. – Richard Mar 04 '22 at 18:58
  • 1
    Related [Colored text in command prompt after "cls" but not before "cls"](https://stackoverflow.com/questions/56792323/colored-text-in-command-prompt-after-cls-but-not-before-cls). – martineau Mar 04 '22 at 20:23