1

I am very new to Python, and wanted to learn how to print coloured text. I don't have any modules like termcolor or colorama, but I found that I can use ANSI escape sequences to do this.

I found a sample code:

print('\x1b[6;30;42m' + 'Success!' + '\x1b[0m')

However, when I run it, it just prints the entire thing like this:

[6;30;42mSuccess![0m

What am I doing wrong, and how can I print coloured texts?

Thank you very much!!

Edit: I am using python 3.7

PTH
  • 51
  • 6
  • 1
    what terminal are you using? Not every terminal supports ANSI escape sequences – Walter Tross Feb 02 '19 at 15:14
  • in case you are on Windows 10, the following might be useful: https://stackoverflow.com/questions/36760127/how-to-use-the-new-support-for-ansi-escape-sequences-in-the-windows-10-console – Walter Tross Feb 02 '19 at 15:30
  • @WalterTross This may sound very stupid, but what exactly do you mean by a terminal, and how do I find which one I'm using? – PTH Feb 02 '19 at 15:30
  • first: what is your operating system. Second: where do you run your python code in, i.e., what is written in the top left corner – Walter Tross Feb 02 '19 at 15:32
  • @WalterTross I'm using macOS, and I'm running the code in IDLE – PTH Feb 02 '19 at 15:35
  • the simplest terminal (and the one I use) in OS X in called... Terminal. Just cmd-space and type terminal. Inside that, `cd` to where you have your code, then try `python .py` or `python3 .py` – Walter Tross Feb 02 '19 at 15:38
  • @WalterTross Thank you! I tried the same code in terminal, and it works. But how do I get it to work in IDLE? – PTH Feb 02 '19 at 15:45
  • as for what a terminal is, here is some history: https://en.wikipedia.org/wiki/Computer_terminal and here is the current state (form a Linux perspective, but the Terminal you have in OS X is very similar): https://askubuntu.com/questions/38162/what-is-a-terminal-and-how-do-i-open-and-use-it And here is the Wikipedia article about Terminal: https://en.wikipedia.org/wiki/Terminal_(macOS) – Walter Tross Feb 02 '19 at 15:46
  • @WalterTross Thank you so much!! – PTH Feb 02 '19 at 15:47
  • I don't know IDLE, sorry, and I prefer not to install it. I use PyCharm (community edition), where ANSI escape codes work. But my advice is to learn to use the terminal, with bash and all the tools you can use from there. – Walter Tross Feb 02 '19 at 15:51
  • Try also the solution in this answer. It is for Windows, but `colorama` seems to be cross-platform: https://stackoverflow.com/questions/12492810/python-how-can-i-make-the-ansi-escape-codes-to-work-also-in-windows – Walter Tross Feb 02 '19 at 15:56
  • @WalterTross Yup, Terminal does seem like the better option. However, my school requires us to use IDLE, so I'll have to make do with it for the time being. And I'll try colorama. Thank you so much for your help so far!! – PTH Feb 02 '19 at 16:01
  • Let us know if colorama is able to convince IDLE to use ANSI escape codes. Also, look into the settings of IDLE and try to find out whether the handling of ANSI escape codes can be enabled – Walter Tross Feb 02 '19 at 16:02

2 Answers2

0

So it turned out you were doing nothing wrong, just executing the Python code in an environment (the IDLE IDE on Mac) which does not recognize (by default?) ANSI escape sequences.

On Mac, ANSI escape sequences are supported, e.g., by Terminal.

Walter Tross
  • 12,237
  • 2
  • 40
  • 64
0

Try this

Blue =  '\033[34m' # Blue Text
print(Blue + 'Hello World')

at the spot where it says 34m that is the colour. red is 31 yellow is 33 purple is 35. and more. THis works for python