Printing Colored Text in Python Without Any Module

Introduction: Printing Colored Text in Python Without Any Module

About: A kid with two arms, two legs, a nose, a head and a torso!!!

After my second Instructable was deleted accidentally, I decided to make a new one.

In this one I will show you how to print colored text in python!

Step 1: The Codes

Try:

TGREEN =  '\033[32m' # Green Text
print (TGREEN + "This is some green text!")

We see that after the green text is printed, the whole shell changes color!

To combat that can we use this?

TWHITE = '\033[37m'

print (TGREEN + "It doens't reset!" , TWHITE)

NO!!

It turns all the text white...and a different type of 'dull' white if you see it carefully.

If you want to commit your code to GITHUB, then many people using your code may have a customized Python Shell with probably YELLOW or Something as their text/background color!

So the answer is:

ENDC = '\033[m' # reset to the defaults

print (TGREEN + "Das ist es!" , ENDC)

Step 2: Usage

The usage goes like:

\033[code;code;codem  # put 'm' at the last
 
\033[code;codem  # use semicolon to use more than 1 code

\033[codem

\033[m   # reset
 

The list of the codes are in the next step.

Step 3: List of Codes

The pictures says it all....though you might find THIS helpful!

I did not mention backgrounds but you can find it HERE.

Just in case you were curious about the code in the intro, here it is.....on GitHub!

Be the First to Share

    Recommendations

    • Tinkercad Student Design Contest

      Tinkercad Student Design Contest
    • Metal Contest

      Metal Contest
    • 3D Printed Student Design Challenge

      3D Printed Student Design Challenge

    Comments