0

How to print out a variable's value in another colour?

Hi guys, so this question is kind of related to the other question that I asked: https://www.sololearn.com/discuss/1603568/?ref=app I mucked around for a while, and I came up with another question: Is there a way to print out a variable on screen in a different color? Let's say I have: [BEG] name = 'Booey' [END] Running: [BEG] print(name) [END] will obviously return: [BEG] Booey [END] But how do I print it out in a different color? I tried: [BEG] print(\033[32mname) [END] And I got: [BEG] >>> print(\033[32mname) File "<stdin>", line 1 print(\033[32mname) ^ SyntaxError: unexpected character after line continuation character [END] Then I tried: [BEG] print('\033[32mname'] [END] and got the output: [BEG] name [END] ... printed in green I want to print out: "my name is Booey", using the variable "name" instead of the string "Booey", and have it printed in another colour. Anyone?

29th Nov 2018, 6:01 AM
D. Booey 📜
D. Booey 📜 - avatar
3 Respostas
+ 1
name = 'Booey' print( "my name is \033[32m" +name +"\033[m") # where "\033[m" in the end, is set off color after print name here is some other basic codes for colour and style https://cdn.instructables.com/FM6/JGYQ/JJZN6S3E/FM6JGYQJJZN6S3E.LARGE.jpg?auto=webp&width=933
29th Nov 2018, 2:17 PM
zemiak
0
Also, I apologize in advance for the messy post. I didn't know how else to include samples and examples. I didn't know how or if I was able to format the text. If there isn't rich formatting, then is there a standard format that you guys use to include snippets of codes and stuff?
29th Nov 2018, 6:03 AM
D. Booey 📜
D. Booey 📜 - avatar
0
zemiak Brilliant! Thanks.. I actually just figured it out myself yesterday and was just going to post a comment to share my findings.. Also, for those that care: If you plan on using a lot of colors, instead of typing out "\033[3?m" every time you want to use a color, you can create a variable for each color. For example, you can do something like : [BEG] cGREEN = '\033[32m' cRED = '\033[31m' cRESET = '\033[m' name = 'Booey' [END] And then use it like: [BEG] print(green + 'my' + cRESET + ' name is ' + cRED + NAME + cRESET) [END]
30th Nov 2018, 12:29 AM
D. Booey 📜
D. Booey 📜 - avatar