2

Is there any way to change shell print color in python 3.3.2 in Linux and Windows shell? I've read all other topics but none of them working with this version or I should import a module. For example I want to print like this with pure python modules:

enter image description here

. I use both of them! And I said none of them working on python 3.3 even: https://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python

And please don't say it's a duplicate question because none of these answers working in python 3.3.2 and I wanted to use a default module to do that not colorama which I should install on python!

Community
  • 1
  • 1
hamidfzm
  • 4,595
  • 8
  • 48
  • 80

4 Answers4

3

These will work on Linux and Mac, but not Windows. I reference them whenever I need to colorize my output.

The first shows how to change the color.

# snippet found at http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python

class bcolors:
    WARNING = '\033[95m'
    ENDC = '\033[0m'

print(bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC)

The second shows how each value displays in your environment.

for i in range(0, 128):
    print("\033[%imThis is text!!\t\t( \\033[%im )\033[0m" % (i, i))
Jack Stout
  • 1,265
  • 3
  • 12
  • 25
1

Based on the description at https://pypi.python.org/pypi/colorama ,

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 otherwise show up as gobbledygook in your output), and converting them into the appropriate win32 calls to modify the state of the terminal.

it sounds like it'd be a non-trivial amount of work to do it yourself.

That said, they also mention this:

An alternative approach is to install 'ansi.sys' on Windows machines, which provides the same behaviour for all applications running in terminals. Colorama is intended for situations where that isn't easy

These links may assist you if ansi.sys is an option:

Rob Starling
  • 3,868
  • 3
  • 23
  • 40
1

To change the color scheme, edit the [Colors] section in config.txt. However, you cannot do this during a program- and even if you could it would be disgustingly inefficient. You need not install any new modules. You can do this through subprocess, a module that comes with Python.

Something like:

from subprocess import call

call('color a', shell=True) #this sets the color to light green
print('The quick brown fox jumps over the lazy dog.')

This works for Windows, and you can easily change the command that is called depending on the operating system. For your program, you could put the colours in a for loop, changing with the messages in the print statement.

As a note, this only works if you run it from its file location or from the command line. It will not work when you run it in IDLE. Hope I helped!

EDIT: You can find a list of colours here. The syntax is: color 'background ID''text ID'.

This would allow you to do something like:

import time
from subprocess import call

for color in('a', 'e', 'c'): #cycles through different colours
    call('cls', shell=True) #clears the screen
    call('color ' + color, shell=True)
    print('The quick brown fox jumps over the lazy dog.')
    time.sleep(1)

input("\nPress enter to exit. ")

From that, you can modify the code to use colours of your choice. Unfortunately, there is absolutely no way no have all the colors on the screen at the same time. For that you DO need external modules, I'm afraid.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
0

Colors in python

Print text nicely or style text using python


# the ANSI codes are stored in variables, making them easier to use
black = "\033[0;30m"
red = "\033[0;31m"
green = "\033[0;32m"
yellow = "\033[0;33m"
blue = "\033[0;34m"
magenta = "\033[0;35m"
cyan = "\033[0;36m"
white = "\033[0;37m"
bright_black = "\033[0;90m"
bright_red = "\033[0;91m"
bright_green = "\033[0;92m"
bright_yellow = "\033[0;93m"
bright_blue = "\033[0;94m"
bright_magenta = "\033[0;95m"
bright_cyan = "\033[0;96m"
bright_white = "\033[0;97m"

print(black + "Hello world")
print(red + "Hello world")
print(green + "Hello world")
print(blue + "Hello world")
print(yellow + "Hello world")
print(magenta + "Hello world")
print(cyan + "Hello world")
print(bright_black + "Hello world")
print(bright_red + "Hello world")
print(bright_green + "Hello world")
print(bright_blue + "Hello world")
print(bright_cyan + "Hello world")
print(bright_magenta + "Hello world")
print(bright_yellow + "Hello world")

output :

View : Colored output

Hritik Jaiswal
  • 311
  • 3
  • 4