2

I am trying to change the colour of a certain line of text. I have looked at other articles (including Change shell print color in python 3.3.2) but none of the work. I would not like to install any external modules if that is possible.

My code:

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.')

I am using Python 3.2.3 on Linux. Thanks for any responses.

Community
  • 1
  • 1
  • Try this: https://pypi.python.org/pypi/colorama. – Chrispresso Dec 21 '15 at 19:15
  • @Dylan, what error it gives you: `/bin/sh: 1: color: not found` ? are you using linux or windows? – Assem Dec 21 '15 at 19:29
  • I am currently using Linux. –  Dec 21 '15 at 19:46
  • @DylanMurphy what error it shows you running your code? – Assem Dec 21 '15 at 19:53
  • 1
    Possible duplicate of [Print in terminal with colors using Python?](https://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python) – Marcy Jun 12 '17 at 10:16
  • Did you try this: https://stackoverflow.com/questions/11108559/how-to-print-colour-color-in-python/48896100#48896100. CLINT or printy or sys is the module you want – mathcat Jul 10 '20 at 10:02

1 Answers1

0

I think the way you are using is aimed at Windows.

You may use termcolor:

from termcolor import colored

print colored('hello', 'red'), colored('world', 'green')

You may use also color formatting codes listed here, use them like that:

>>> text = "The quick brown fox jumps over the lazy dog."
>>> print('\033[1;32m'+text+'\033[1;m')
The quick brown fox jumps over the lazy dog.

Output would be like:

enter image description here

Assem
  • 10,850
  • 5
  • 53
  • 90
  • Does not work but thanks anyway. I will still mark this as the best answer. –  Dec 21 '15 at 20:02
  • You have to install termcolor first: in Windows Powershell pip install termcolor, or Mac Terminal: pip3 install termcolor – Valy Jun 10 '21 at 15:27