Program Python untuk mencetak teks berwarna ke terminal


Contoh 1: Menggunakan urutan escape ANSI

cetak (' x1b[38;2;5;86;243m' + 'Programiz' + 'x1b[0m')



The working of the above line of code is shown in the figure below.

Code for colored terminal
Code for colored terminal

Let’s understand the escape code x1b[38;2;5;86;243m.

  • x1b calls a function. You can also use 33 for the same purpose.
  • 38;2;r;g;b helps to set RGB color. 5;86;243 are the rgb color for blue (the color of the logo of Programiz).
  • m is the function name. Here, m means SGR (Select Graphics Rendition) function.

For more information regarding the ANSI escape code, you can refer to ANSI escape code.

Example 2: Using python module termcolor

from termcolor import colored

print(colored('Programiz', 'blue'))



Using the module termcolor, you can get the desired output. Also, you can set different styles of the text using this module.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.