2

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?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
mvsat
  • 31
  • 1
  • 1
  • 4
  • 1
    Please consider using a different variable name for your list.. list is not an enforced keyword but it will give you unforeseeable problems if you use that as your variable name.. – ababuji May 22 '18 at 05:20
  • 1
    Possible duplicate of [How do I print colored/bold output to the terminal in Python?](https://stackoverflow.com/questions/37340049/how-do-i-print-colored-bold-output-to-the-terminal-in-python) – smci May 22 '18 at 05:27
  • You are making the bold for square bracket also if it is really want print bold before printing the array – mkHun May 22 '18 at 05:27

3 Answers3

6

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))
Austin
  • 25,759
  • 4
  • 25
  • 48
  • Please close as duplicate [How do I print colored/bold output to the terminal in Python?](https://stackoverflow.com/questions/37340049/how-do-i-print-colored-bold-output-to-the-terminal-in-python) – smci May 22 '18 at 05:27
  • I need the output to be enclosed in list. Inside the list only, the bold letters are not coming – mvsat May 22 '18 at 05:32
  • Wow! It helps. Thanks. – mvsat May 22 '18 at 05:37
1

try printing as follows

for each in list:
    print(each)

following is the output enter image description here

rawwar
  • 4,834
  • 9
  • 32
  • 57
  • 2
    I did not downvote, and I know that it will work. But code only answers are considered as poor on SO. Explaining **why** it works that way will greatly improve your answer... – Serge Ballesta May 22 '18 at 05:26
  • Your code will print all outputs in bold even if an item doesn't prefix with the escape string because it didn't ends with '\033[0m'. – Ancora Imparo May 22 '18 at 05:29
  • I need the output to be enclosed in list. Inside the list only, the bold letters are not coming @Kalyan – mvsat May 22 '18 at 05:34
  • are you trying to do it in ipython console? – rawwar May 22 '18 at 06:13
0

Kalyan gave a possible way, but failed to explain the why.

When ask Python to print a list, it will output the start and end of list markers([]) and a representation of all the items of the list. For a string, it means that non printable characters will be escaped.

There is little you can do except printing separately the items

for i lst:
    print i

or build a unique string with join:

string_to_output = '[' + ', '.join(lst) + ']'
print(string_to_output)

By the way, as explained by Austin '\x01b[1m is an sequence to ask an ANSI compatible terminal to pass in bold mode, so you must at a time revert to normal mode with '\x01b[0m.

And remember: ANSI is common in Linux terminal emulations, but it is still far from being universal... Just try to use export TERM=tvi950 before using it (and google for vt100 and tvi950 to understand more)...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252