Click here to Skip to main content
15,923,142 members
Articles / Programming Languages / Python
Tip/Trick

How to Put Color on Windows Console

Rate me:
Please Sign up or sign in to vote.
4.81/5 (26 votes)
19 Aug 2020CPOL2 min read 152.6K   905   40   43
How to put color in Python, C, C++, C#, Java and batch on the Windows console
In this article, you will see how to use ANSI colors in Windows and Linux without any modification on code, with Python, C, C++, C# and Java, and how to put color on batch (Command Prompt).

Introduction

Now Windows have support for ANSI colors!

On old builds (Windows 10 1909 or prior), Windows just recognizes the ESC code 1, but it is possible for Windows to recognize the ESC code \033 (or other depending on the language) with a modification in the record (regedit.exe).

The Modification

For modification, you need to run the command below (on CMD as administrator):

BAT
reg add HKEY_CURRENT_USER\Console /v VirtualTerminalLevel /t REG_DWORD /d 0x00000001 /f

In case of error (it'll have no error, but...), here is the command to undo: (on CMD as administrator)

BAT
reg add HKEY_CURRENT_USER\Console /v VirtualTerminalLevel /t REG_DWORD /d 0x00000000 /f

With this modification, you can compile the same program for Windows and Linux without changing anything; before to color the text of the console on Python for Linux, you used the ESC code, on Windows you used a library, so you had to write the same program twice!

Colors

Color Foreground Background
Black <ESC>[30m <ESC>[40m
Red <ESC>[31m <ESC>[41m
Green <ESC>[32m <ESC>[42m
Yellow <ESC>[33m <ESC>[43m
Blue <ESC>[34m <ESC>[44m
Magenta <ESC>[35m <ESC>[45m
Cyan <ESC>[36m <ESC>[46m
Light gray <ESC>[37m <ESC>[47m
Dark gray <ESC>[90m <ESC>[100m
Light red <ESC>[91m <ESC>[101m
Light green <ESC>[92m <ESC>[102m
Light yellow <ESC>[93m <ESC>[103m
Light blue <ESC>[94m <ESC>[104m
Light magenta <ESC>[95m <ESC>[105m
Light cyan <ESC>[96m <ESC>[106m
White <ESC>[97m <ESC>[107m
Bold <ESC>[1m -
Underline <ESC>[4m -
No underline <ESC>[24m -
Negative(reverse the foreground and background) <ESC>[7m -
Positive(no negative) <ESC>[27m -
Default <ESC>[0m -

To use a color, you need to write the ANSI color, your text and the ANSI color default.

Here goes an example of this in Python:

Python
print("\033[31mThis is red\033[0m")

In C:

C++
int main() {
    printf("\033[33mThis is yellow\033[0m");
    return 0;
}

In C++:

C++
#include <iostream>

int main() {
    std::cout << "\033[32mThis is green\033[0m" << std::endl;
    return 0;
}

In C#:

C#
class Program {
    static void Main() {
        System.Console.WriteLine("\u001b[35mThis is purple\u001b[0m");
    }
}

In Java:2

Java
public class Color {
    public static void main(String[] args) {
        System.out.println("\033[36mThis is cyan\033[0m");
    }
}

And in batch:

BAT
echo This is blue

Access this site for more information and others ANSIs.

Where Does It Work?

This method of the modification works (that I tested) in Python, C, C++ and Java, but doesn't work in batch, there the <ESC> has to be , and in C#, there the <ESC> has to be \u001b.

Why Would I Use That?

With this method, you can compile for Linux without modification in Python, C, C++, C# and Java.

Notes

1 The character is a rectangle, if a question appears for you, just copy and paste it into Notepad and it will appear correctly.

2 If you click on jar file, nothing will happen, to run you have to call from the prompt, for that run the command lower in the folder that is jar file:

BAT
java -jar "program.jar"

where program.jar is the name of the jar file.

History

  • 1st January, 2020: Initial version
  • 21st February, 2020: C# update
  • 9th March, 2020: Java update
  • 18th August, 2020: colors' update

Read My Other Article

If you have any comments, please leave a note below.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Brazil Brazil
I'm a student of programming and math, a chess player and a person who by nature loves to solve problems

Comments and Discussions

 
QuestionHigh Color on WSL console Pin
Member 146691693-Feb-20 8:05
Member 146691693-Feb-20 8:05 
QuestionHow to Put Color in Windows Pin
MARK ody23-Jan-20 21:17
MARK ody23-Jan-20 21:17 
AnswerRe: How to Put Color in Windows Pin
Tiago Cavalcante Trindade24-Jan-20 2:17
Tiago Cavalcante Trindade24-Jan-20 2:17 
AnswerRe: How to Put Color in Windows Pin
Richard MacCutchan4-Feb-20 6:20
mveRichard MacCutchan4-Feb-20 6:20 
QuestionDon't Understand Pin
E. Papulovskiy21-Jan-20 21:01
E. Papulovskiy21-Jan-20 21:01 
AnswerRe: Don't Understand Pin
Tiago Cavalcante Trindade22-Jan-20 2:34
Tiago Cavalcante Trindade22-Jan-20 2:34 
PraiseMy vote of 5 Pin
Michael Haephrati19-Jan-20 4:20
professionalMichael Haephrati19-Jan-20 4:20 
GeneralRe: My vote of 5 Pin
Tiago Cavalcante Trindade20-Jan-20 3:41
Tiago Cavalcante Trindade20-Jan-20 3:41 
Thanks for your vote.
I add more options for text formatting and add a downloadable version.
GeneralMy vote of 4 Pin
schlebe2-Jan-20 9:03
schlebe2-Jan-20 9:03 
GeneralRe: My vote of 4 Pin
Tiago Cavalcante Trindade13-Jan-20 8:41
Tiago Cavalcante Trindade13-Jan-20 8:41 
GeneralRe: My vote of 4 Pin
schlebe13-Jan-20 10:35
schlebe13-Jan-20 10:35 
GeneralRe: My vote of 4 Pin
Tiago Cavalcante Trindade13-Jan-20 12:00
Tiago Cavalcante Trindade13-Jan-20 12:00 
GeneralRe: My vote of 4 Pin
David O'Neil28-Jan-20 11:34
professionalDavid O'Neil28-Jan-20 11:34 
GeneralRe: My vote of 4 Pin
Tiago Cavalcante Trindade31-Jan-20 10:53
Tiago Cavalcante Trindade31-Jan-20 10:53 
GeneralRe: My vote of 4 Pin
David O'Neil31-Jan-20 11:50
professionalDavid O'Neil31-Jan-20 11:50 
GeneralA 5 for you... Pin
David O'Neil2-Feb-20 19:43
professionalDavid O'Neil2-Feb-20 19:43 
GeneralRe: A 5 for you... Pin
Tiago Cavalcante Trindade3-Feb-20 6:20
Tiago Cavalcante Trindade3-Feb-20 6:20 
GeneralRe: A 5 for you... Pin
David O'Neil3-Feb-20 6:24
professionalDavid O'Neil3-Feb-20 6:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.