Change Background Color of String in Python

  • Post category:String

The string format() method is used to change background color of string in Python . The below code 41m{} is used to change the background color of a string to red.

The list of background colors is given below.

40m = Black

41m = Red

42m = Green

43m = Yellow

44m = Blue

45m = Magenta

46m = Cyan

47m = White

48m = Reset

Step 1: Create a string

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

Step 2: Background color string

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

Output:

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

Free resources to learn advanced skills: AiHints and CodeAllow