Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Probably not possible but.. Colour changing in shell

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.
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!
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") 
(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
(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.
You can do it in the CMD shell, not the IDE shell.
[Image: 5BgQiIm.gif]

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?
Colorama is the way to go. It is available for both python 2 and 3.
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
(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?
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 :)
(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?
(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
(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.
(May 2, 2017 03:35 AM)SIM-1866423 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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by Thoma$Aug 11, 2016 11:52 AM 12 259 Aug 11, 2016 11:52 AM
Last Post: +Haze
[C] by CoughApr 3, 2016 11:17 AM 6 203 Apr 3, 2016 11:17 AM
Last Post: 0xDEAD10CC
[.NET] by ChessoMar 11, 2015 08:50 AM 7 289 Mar 11, 2015 08:50 AM
Last Post: ElementalTree