Bold formatting in Python console

14,364

Solution 1

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))

Solution 2

try printing as follows

for each in list:
    print(each)

following is the output enter image description here

Share:
14,364

Related videos on Youtube

mvsat
Author by

mvsat

Updated on June 04, 2022

Comments

  • mvsat
    mvsat about 2 years

    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?

    • imperialgendarme
      imperialgendarme about 6 years
      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..
    • smci
      smci about 6 years
    • mkHun
      mkHun about 6 years
      You are making the bold for square bracket also if it is really want print bold before printing the array
  • Serge Ballesta
    Serge Ballesta about 6 years
    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...
  • smci
    smci about 6 years
  • Ancora Imparo
    Ancora Imparo about 6 years
    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'.
  • mvsat
    mvsat about 6 years
    I need the output to be enclosed in list. Inside the list only, the bold letters are not coming
  • mvsat
    mvsat about 6 years
    I need the output to be enclosed in list. Inside the list only, the bold letters are not coming @Kalyan
  • mvsat
    mvsat about 6 years
    Wow! It helps. Thanks.
  • rawwar
    rawwar about 6 years
    are you trying to do it in ipython console?