Colours in Python

nypon

New Member
Joined
Jul 7, 2026
Messages
3
Reaction score
0
Credits
30
So when I run Python in the terminal and type some code in, it's all white. No colours no matter what type of command/whatever I type. I read that I could maybe install something called 'blessings' by typing 'pip install blessings', and then use it in a script by typing 'from blessings import Terminal'. I wanted to double check whether that's the best way to do things, and safe etc? Appreciate any feedback.
 


There are a number of ways to get coloured text in python.
The easiest is just to use ANSI escape codes, which are built in terminal codes that will change the colour when they are printed to the screen.
I don't have time to create any examples right now, but a quick bit of duckduckgo-fu yielded this tutorial, which gives you a few different options:
 
There are a number of ways to get coloured text in python.
The easiest is just to use ANSI escape codes, which are built in terminal codes that will change the colour when they are printed to the screen.
I don't have time to create any examples right now, but a quick bit of duckduckgo-fu yielded this tutorial, which gives you a few different options:
Hi! Thanks for your reply. I'm a bit confused by the above tutorial though, as it seems like it's for manually formatting text with different colours if I understand correctly. What I'd like is just to have the standard Python colours corresponding with certain key words (like 'print' being green), not to write a text string with a certain colour, for example. Not for style, but for clarity. Is there any way to do that?
 
Hi! Thanks for your reply. I'm a bit confused by the above tutorial though, as it seems like it's for manually formatting text with different colours if I understand correctly. What I'd like is just to have the standard Python colours corresponding with certain key words (like 'print' being green), not to write a text string with a certain colour, for example. Not for style, but for clarity. Is there any way to do that?
It sounds like you are referring to syntax highlighting, which means, in the case of python code, when opening a file with python code in a terminal, the various key words and variables are colored consistently throughout the file. If one opens a file of python code in nano, e.g. nano some_file_of_python_code.py, then it's usually colored by default.

In the vim editor, one can configure syntax highlighting with the configuration: syntax on, in the vim configuration file in
/home/$USER/.vimrc file.

Both nano and vim can highlight the syntax of numerous programming languages. Here's an example of nano using color for a file of python code:

syntax.png


Here is an example of vim using syntax highlighting on the same file:

syntax2.png

The actual colors used for highlighting can themselves be configured in the configuration files if one prefers to use non-default colors.
 
Last edited:
It sounds like you are referring to syntax highlighting, which means, in the case of python code, when opening a file with python code in a terminal, the various key words and variables are colored consistently throughout the file. If one opens a file of python code in nano, e.g. nano some_file_of_python_code.py, then it's usually colored by default.

In the vim editor, one can configure syntax highlighting with the configuration: syntax on, in the vim configuration file in
/home/$USER/.vimrc file.

Both nano and vim can highlight the syntax of numerous programming languages. Here's an example of nano using color for a file of python code:

View attachment 32543

Here is an example of vim using syntax highlighting on the same file:

View attachment 32544
The actual colors used for highlighting can themselves be configured in the configuration files if one prefers to use non-default colors.
That sounds and looks right on the money. Now could you maybe explain like I'm five how I get that to work? I've looked at a tutorial and managed to do the thing part of the way. I now have a python file in Desktop that if I open and type in, will have the correct words highlighted. Of course it's not in Python, it's just text so I can't run it. But if I open my Terminal and open Python, the text is still white as before. Am I missing something?
 


Follow Linux.org

Staff online


Latest posts

Top