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!

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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

    • Trash to Treasure Contest

      Trash to Treasure Contest
    • Raspberry Pi Contest 2020

      Raspberry Pi Contest 2020
    • Wearables Contest

      Wearables Contest

    Discussions