Color 'print' in Python

Question:

Been going thru this: How do I print colored text to the terminal?. My issue is little different than those solutions (or I just can’t find it). I need to print two variables in different colors in same print statement.

For example
print("{0} {1}".format(test1, test2))
Should print ‘one’ in RED and ‘two’ in BLUE.

Below works for single line. But how do I combine them.

os.system("")
class style():
  RED = '33[31m'
  GREEN = '33[32m'
  BLUE = '33[34m'
  RESET = '33[0m'

test1 = "ONE"
test2 = "TWO"

print(style.RED + "{0}".format(test1) + style.RESET)
print(style.GREEN + "{0}".format(test2) + style.RESET)
Asked By: AliasSyed

||

Answers:

You can use f-strings:

print(f"{style.RED}{test1} {style.BLUE}{test2}{style.RESET}")
Answered By: Jonathan Ciapetti

You could use the print statement’s built in end parameter instead of os. This is how you would do it:

class style():    
    RED = '33[31m'
    GREEN = '33[32m'
    BLUE = '33[34m'
    RESET = '33[0m'

test1 = "ONE"
test2 = "TWO"

print(style.RED + "{0}".format(test1) + style.RESET, end='')
print(style.GREEN + "{0}".format(test2) + style.RESET, end='')

And no, I don’t think you can combine them.

Another thing. Your class notation is not good. Normally, you should use the init function to initialize your variables.

In this case, a class is a bad idea to store those strings. I think you should define those as variables instead of in a class, like follows:

RED = '33[31m'
GREEN = '33[32m'
BLUE = '33[34m'
RESET = '33[0m'

test1 = "ONE"
test2 = "TWO"

print(RED + "{0}".format(test1) + RESET, end='')
print(GREEN + "{0}".format(test2) + RESET, end='')

If you don’t mind, allow me to refactor your code to make it more pythonic and eventually more readable:

class Style():
  RED = "33[31m"
  GREEN = "33[32m"
  BLUE = "33[34m"
  RESET = "33[0m"

print(f"{Style.RED}ONE{Style.RESET}")
print(f"{Style.GREEN}TWO{Style.RESET}")

You can combine the strings like this:

red_text = f"{Style.RED}ONE{Style.RESET}"
green_text = f"{Style.GREEN}TWO{Style.RESET}"

print(f"{red_text} {green_text}")

F-strings were introduced in Python 3.6. If you’re using an earlier version of Python, you can simply use string concatenation with a + between strings (instead of the less readable format() function):

print(Style.RED + "ONE" + Style.RESET)

red_text = Style.RED + "ONE" + Style.RESET

Instead of coding everything from scratch, you could also check other Python packages. For example Colorist – a lightweight package that enables you to add style and colour to text messages in the terminal (in full disclosure, I’m the author of this package). This may simplify your code:

from colorist import red, green

red("ONE")
green("TWO")

enter image description here

Another variation if you want to keep the f-strings. The output is the same:

from colorist import Color

print(f"{Color.RED}ONE{Color.OFF}")
print(f"{Color.GREEN}TWO{Color.OFF}")
Answered By: Jakob Bagterp
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.