1

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 = '\033[31m'
  GREEN = '\033[32m'
  BLUE = '\033[34m'
  RESET = '\033[0m'

test1 = "ONE"
test2 = "TWO"

print(style.RED + "{0}".format(test1) + style.RESET)
print(style.GREEN + "{0}".format(test2) + style.RESET)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
AliasSyed
  • 67
  • 8

3 Answers3

2

You can use f-strings:

print(f"{style.RED}{test1} {style.BLUE}{test2}{style.RESET}")
Jonathan Ciapetti
  • 1,261
  • 3
  • 11
  • 16
1

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

class style():    
    RED = '\033[31m'
    GREEN = '\033[32m'
    BLUE = '\033[34m'
    RESET = '\033[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 = '\033[31m'
GREEN = '\033[32m'
BLUE = '\033[34m'
RESET = '\033[0m'

test1 = "ONE"
test2 = "TWO"

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

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

class Style():
  RED = "\033[31m"
  GREEN = "\033[32m"
  BLUE = "\033[34m"
  RESET = "\033[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}")
Jakob Bagterp
  • 450
  • 4
  • 12