Whether it's a module or something I can define in my code, is there a way to change the output text like making it italic or BOLD? Also, are colours a thing?
Asked
Active
Viewed 3,697 times
0
-
Maybe take a look at this answer http://stackoverflow.com/a/17303428/2999446 – Luke K Sep 01 '16 at 22:42
-
thank you! @LukeK, i did look but didnt find anything like this. – Elijah D-R Sep 01 '16 at 22:44
-
@LukeK it doesnt have italics :/ – Elijah D-R Sep 01 '16 at 22:45
-
Possible duplicate of [How do I print bold text in Python?](http://stackoverflow.com/questions/8924173/how-do-i-print-bold-text-in-python) – Flint Sep 01 '16 at 22:46
-
2Do you mean the appearance of text in your system's shell/console? Or in a text file that you generate with Python? Or something else? – TigerhawkT3 Sep 01 '16 at 22:47
-
1Code for italics is \x1B[3m. Add something like ITALIC = "\x1B[3m" to the class they have there – Luke K Sep 01 '16 at 22:47
1 Answers
0
Taking the question @LukeK posted, you can use ANSI escape sequences anywhere in your strings:
print("Roses are \033[31mred\033[0m") # Will make red... red
You can combine them in that way (I hope it's clear enough):
BOLD_AND_RED = "\033[1;31m"
On the other hand, you can find modules that allow simpler (or nicer) ways of doing it, but I prefer writing code with minimal dependencies, so I usually put some global variables and I later concatenate them with my strings:
RED = "\033[31m"
BOLD = "\033[1m"
ITALIC = "\033[3m"
RESET = "\033[0m"
print(BOLD + RED + "ERROR: " + RESET + ITALIC + "blah blah...")
Last note! This escape sequences only work in terminals, and I don't think they work in Windows cmd.

José Miguel
- 280
- 3
- 7