-1

I am writing a print statement that prints a single word in the whole string as a different color. Does anyone know how I can do that? The method I am using prints only the highlighted word.

import random


def skyBlue(skk): print("\033[96m {}\033[00m" .format(skk))

lsubject = ["physics", "geology", "history", "algebra", "literature"]


subjectColor = skyBlue(random.choice(lsubject))
print("You study " + str(subjectColor) + ".")
A Space
  • 3
  • 2
  • 4
    Your function should return the string, not print it. – Michael Butscher Jun 28 '20 at 04:56
  • @Zeek I voted to reject your edit because the type of `lsubject` is not known, and `random.choice` doesn't really make sense on a string that has repeat characters. I think it's supposed to be a list of strings. In any case, `random.choice` is probably irrelevant anyway. – wjandrea Jun 28 '20 at 05:03
  • Does this answer your question? [Python Script returns unintended "None" after execution of a function](https://stackoverflow.com/questions/16974901/python-script-returns-unintended-none-after-execution-of-a-function) – wjandrea Jun 28 '20 at 05:06
  • Run your code again. It actually prints the highlighted word, then `You study None.`. Colorizing has nothing to do with the problem; really you're asking how to get a string back from a function. Also, beside the point, but `print` is a function in Python 3, not a statement. BTW welcome to SO! Check out the [tour] and [ask]. – wjandrea Jun 28 '20 at 05:08
  • Sorry for not being clear - forgot to add some variables. And I am printing a random word from a list. – A Space Jun 28 '20 at 17:53

1 Answers1

2

You can just return the string that you have already formatted.

def skyBlue(skk): return "\033[96m {}\033[00m".format(skk)
subjectColor = skyBlue(random.choice(lsubject))
print("You study " + subjectColor + ".")
ddastrodd
  • 710
  • 1
  • 10
  • 22