DO NOT DEAL WITH THIS MEMBER
Apr 13, 2017 03:20 PM
(This post was last modified: Apr 13, 2017 03:24 PM by Skidy.)
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.
DO NOT DEAL WITH THIS MEMBER
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")
DO NOT DEAL WITH THIS MEMBER
(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:
(Apr 13, 2017 03:50 PM)Skidy Wrote: That didn't work for some reason.
I get this:
It only works on Linux and maybe mac not Windows.
You can do it in the CMD shell, not the IDE shell.
Apr 13, 2017 04:55 PM
(This post was last modified: Apr 13, 2017 04:57 PM by Pylon.)
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.
DO NOT DEAL WITH THIS MEMBER
(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?
Apr 14, 2017 07:14 PM
(This post was last modified: Apr 14, 2017 07:16 PM by ObliviousCore.)
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 :)
DO NOT DEAL WITH THIS MEMBER
(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?
DO NOT DEAL WITH THIS MEMBER
(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
DO NOT DEAL WITH THIS MEMBER
(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.
DO NOT DEAL WITH THIS MEMBER
(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.
|