Hack Forums
[Python] Probably not possible but.. Colour changing in shell - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Probably not possible but.. Colour changing in shell (/showthread.php?tid=5599119)



Probably not possible but.. Colour changing in shell - Skidy - Apr 13, 2017

So, in the Python shell is it possible to change specific print statements color?

print("Hello") in red.. etc.. etc..

I only question this as I have seen a few things online but none seem to work


Without installing some other library such as colorama.


* - /usr - Apr 13, 2017

Hi,
you should install some lib, like the one you stated (colorama) or termcolor.
I've found this webpage which explain very well: since I've started learning python not so long ago, I'll paste the link here, because it can explain better than me ;) https://pypi.python.org/pypi/colorama
In any case, on Windows, remember to import init, or it won't work.

Hope you'll find a solution!


* - systx - Apr 13, 2017

http://stackoverflow<.>com/questions/287871/print-in-terminal-with-colors-using-python

PHP Code

<?php 
class bcolors:
    
color = '\033[95m'
print (bcolors.color + "This a color") 



* - Skidy - Apr 13, 2017

(Apr 13, 2017 03:47 PM)systx Wrote:  http://stackoverflow<.>com/questions/287871/print-in-terminal-with-colors-using-python

PHP Code

<?php 
class bcolors:
    
color = '\033[95m'
print (bcolors.color + "This a color") 

That didn't work for some reason.
I get this:

Code
This a color



* - systx - Apr 13, 2017

(Apr 13, 2017 03:50 PM)Skidy Wrote:  That didn't work for some reason.
I get this:

Code
This a color

It only works on Linux and maybe mac not Windows.


* - TheWarOwl - Apr 13, 2017

You can do it in the CMD shell, not the IDE shell.


* - Pylon - Apr 13, 2017

If you are using the Terminal you can use this:

PHP Code

<?php 
class cPrint():
    
HEADER = '\033[95m'
    
OKBLUE = '\033[94m'
    
OKGREEN = '\033[92m'
    
WARNING = '\033[93m'
    
FAIL = '\033[91m'
    
ENDC = '\033[0m'
    
BOLD = '\033[1m'
    
def header(self, output):
        print 
"{}{}{}".format(self.HEADER, output, self.ENDC)
    
def okblue(self, output):
        print 
"{}{}{}".format(self.OKBLUE, output, self.ENDC)
    
def okgreen(self, output):
        print 
"{}{}{}".format(self.OKGREEN, output, self.ENDC)
    
def fail(self, output):
        print 
"{}{}{}".format(self.FAIL, output, self.ENDC)
    
def bold(self, output):
        print 
"{}{}{}".format(self.BOLD, output, self.ENDC)
# this is a test to make sure it is working
Print = cPrint()
Print.
header('Hello Word!')
Print.
okblue('Hello Word!')
Print.
okgreen('Hello Word!')
Print.
fail('Hello Word!')
Print.
bold('Hello Word!') 

Hope this helps,
Good luck!



(Apr 13, 2017 04:07 PM)systx Wrote:  It only works on Linux and maybe mac not Windows.

Yes, this works on Macs OSX 👍


——————————————————————
If you want to learn Python, Join Me! ( #/96jKkqY )
How do you like my redneck signature?



* - Goodies - Apr 13, 2017

Colorama is the way to go. It is available for both python 2 and 3.


* - Skidy - Apr 14, 2017

(Apr 13, 2017 06:59 PM)Goodies Wrote:  Colorama is the way to go. It is available for both python 2 and 3.
Will it work in the IDE?


* - ObliviousCore - Apr 14, 2017

PHP Code

<?php 
from colorama import init
, Fore, Back, Style
init
()
print 
Fore.RED + 'hello world' 
hope this works for you, look into the documentation for more information about the colors :)


* - Skidy - May 1, 2017

(Apr 13, 2017 06:59 PM)Goodies Wrote:  Colorama is the way to go. It is available for both python 2 and 3.

Will this work for both mac and windows though?


* - SIM-1866423 - May 2, 2017

(May 1, 2017 03:37 PM)Skidy Wrote:  Will this work for both mac and windows though?

"ANSI escape character sequences have long been used to produce colored terminal text and cursor positioning on Unix and Macs. Colorama makes this work on Windows, too, by wrapping stdout, stripping ANSI sequences it finds (which would appear as gobbledygook in the output), and converting them into the appropriate win32 calls to modify the state of the terminal. On other platforms, Colorama does nothing."

https://github.com/tartley/colorama


* - /usr - May 2, 2017

(May 1, 2017 03:37 PM)Skidy Wrote:  Will this work for both mac and windows though?

Yes, try using also termcolor, this sometimes is easier.


* - Skidy - May 2, 2017

(May 2, 2017 03:35 AM)Mnemosyne Wrote:  "ANSI escape character sequences have long been used to produce colored terminal text and cursor positioning on Unix and Macs. Colorama makes this work on Windows, too, by wrapping stdout, stripping ANSI sequences it finds (which would appear as gobbledygook in the output), and converting them into the appropriate win32 calls to modify the state of the terminal. On other platforms, Colorama does nothing."

https://github.com/tartley/colorama

I read that shortly after making that post. Will close as this has been resolved.