one of the first modules I’m working on in a python program I’m putting together is a journal.

I’m getting comfortable using the print function, but for this project it would be great if i Could differentiate some of the text output from others. For example, here’s a text output sample from the program:

November_29_2017 Log_1 11/29/17-Completed Homework Log_2 11/29/17-Did 30min of Cardio

In that sample, November_29_2017 is a printed dictionary label. What I would like to do is have that output in a different color instead of the standard output color, so that way I can differentiate it as a label or headline in my journal.

I don’t know the exact magnitude of this task, but if I could at least be provided with some research materials, I’d be more than willing and appreciative to get this going. Thank you!


Recently I utilized ANSI escape codes to get the color on my print-outs, but this method is limited for me, because it seems to only work with string literals, and not printable objects like list and dictionaries. For example, here is an ANSI escape code to color a string literal:

print '\033[1;34mBlue like Blood\033[1;m'

The output would be the string "Blue like blood" colored blue. But, what if I had a list:

L=["Blue like Blood","Orange bananas and red monkeys."]

Now, it would be time consuming typing all the text in the list object directly into the function and passing it, what would be better is if I could pass the list object through the function using it's index(or key if it's a dict object).

I'm still a rookie in Python, but I believe I know enough that simply trying to slap a list or dict call between an escape code isn't going to work(I tried it).

What I think I need is a method for print() that automatically colors the text of any object passed through the function, like I said, I'm a rookie but I believe it would look something like this:


Terms like: Mapping, "create callable objects", classes,etc., I don't get yet but...well I really don't know but I feel like I'm close. Please, still looking for info, and thanks to the people that have provided input.

| improve this question | | | | |
  • 2
    You would probably have better luck with this question on Stack Overflow. – Mark Benningfield Dec 24 '17 at 0:28
  • Are you printing the output directly to the console or are you writing to a file or GUI window? That makes a world of difference for the possibilities of coloring/highlighting/formatting the output. – Bart van Ingen Schenau Dec 24 '17 at 7:53
  • @BartvanIngenSchenau I’m printing directly to console. My program is pretty simple at it’s current stage, it can be ran completely from within terminal/command-prompt. I want to see color on some of its output within the terminal. I have been reading about things like Colorama and Colored. I’m about to install the Colored package on to my Pi, see if that can get it done. – Iam Pyre Dec 24 '17 at 8:36
  • 1
    You can likely use the ANSI color escape sequences. Try print("\033[31mHello\033[0m World") on the target terminal to see if they're supported. – Patrick Haugh Dec 24 '17 at 18:47
  • 1
    simply trying to slap a list or dict call between an escape code isn't going to work(I tried it) - sure you can add escape sequences to an object like a list, when you convert your list to a string first, see this SO post, or google for "python list to string" to find plenty of sources. Note ANSI escape codes don't work in every environment. – Doc Brown Jan 2 '18 at 15:18

Recently I utilized ANSI escape codes to get the color on my print-outs, but this method is limited for me, because it seems to only work with string literals, and not printable objects like list and dictionaries.

The comments you gave indicated you simply got the syntax wrong. Try a function like this one:

def blueprint(x):
    print('\033[34m' + str(x) + '\033[0m')

(The parentheses for print are necessary in Python 3, and optional in Python 2).

Applying this to your example

 L=["Blue like Blood","Orange bananas and red monkeys."]

should do the trick, you can actually pass everything into that function which can be converted to a string by str.

Note this will only work in a terminal window which supports ANSI color codes. Under Linux or Mac, this should not be a problem, as well as in a Windows 10 environment, and for earlier Windows versions, the Colorama module (thanks to @tale852150 for the link) should solve this in a platform-independent manner.

As to your original problem: creating a journal which will only display correctly when printed out in an ANSI terminal is IMHO a very limited approach. If you ever try to save your journal to a file, and then open it in a text editor, you will only see escape sequences and none of the colors. A more standard way of doing these things today is utilizing HTML, writing your journal to an HTML file and use a web browser for displaying it. That way, you could mark headlines using the <head> tag, and format them with any color you like using cascading style sheets.

| improve this answer | | | | |

Give this a try Colorama. This may be what you are looking for and it works on Windows.

Here is some example code:

from colorama import Fore, Back, Style
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print('back to normal now')

Here is an example with a list:

Color text in Python list

To install colorama on RHEL:

pip install colorama

Also this example right here in SO:

Print to terminal in colors with Python

| improve this answer | | | | |
  • This is probably helpful for the OP, but still far away from a good answer. – Doc Brown Jan 2 '18 at 16:49
  • @DocBrown - appreciate the feedback. How can I make it better? – tale852150 Jan 2 '18 at 16:51
  • Well, the OP explicitly asked about how to colorize a list or a dict. A short example how to do this would probably answer that main part of the question. – Doc Brown Jan 2 '18 at 17:05
  • That is not an example how to colorize a list or a dict. Did you read the question thoroughly? – Doc Brown Jan 2 '18 at 18:40
  • @DocBrown - I wanted to give a general example. Then an example with a list. – tale852150 Jan 2 '18 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.