0

keyword_list = ['cat', 'dog']

string = 'I have a dog and a cat'

Say I have defined keyword_list and string, how can i print the string with keywords bold:

'I have a dog and a cat'

Trent
  • 53
  • 1
  • 7

1 Answers1

0

Use replace to add bold to the front of the target text and then end the escape with the end chars on the other side of each target text:

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

keyword_list = ['cat', 'dog']

string = 'I have a dog and a cat'

for i in keyword_list:
   string = string.replace(i, BOLD + i + END)

print(string)

More info here

leopardxpreload
  • 767
  • 5
  • 17