Bold formatting in Python console

mvsat picture mvsat · May 22, 2018 · Viewed 12.7k times · Source

I have defined a list in Python code.

list = ['kumar','satheesh','rajan']    
BOLD = '\033[1m'

for i, item in enumerate(list):
   list[i] = BOLD + item

print (list)

But I am getting the output as ['\x1b[1mfsdfs', '\x1b[1mfsdfsd', '\x1b[1mgdfdf']

But the required output is ['kumar','satheesh','rajan']

How to format the list elements in bold using python?

Answer

Austin picture Austin · May 22, 2018

You need to also specify END = '\033[0m':

list = ['kumar','satheesh','rajan']

BOLD = '\033[1m'
END = '\033[0m'

for each in list:
    print('{}{}{}'.format(BOLD, each, END))

To make the list itself bold like ['kumar', 'satheesh', 'rajan']:

print('{}{}{}'.format(BOLD, list, END))