Change String Color in Python

  • Post category:String

The String format() method is used to change string color. In the below code, 31m{} is used to change the color of a string to red.

The list of colors is given below.

30m = Black

31m = Red

32m = Green

33m = Yellow

34m = Blue

35m = Magenta

36m = Cyan

37m = White

38m = Reset

Step 1: Create a string

str = "CodeAllow"
print("str = ", str)
print("str type = ", type(str))

Step 2: Color string

print("str = ", "\033[31m{}\033[0m".format(str))

Output:

str =  CodeAllow
str type =  <class 'str'>
str =  CodeAllow

Free resources to learn advanced skills: AiHints and CodeAllow