0

I would like for the entire text that pops up when you use the help() function to be affected by the color code that I used, but instead when I run the script the only part that is affected by said color code is a "None" text below the help() result

print(f'\033[34m {help("for")} \033[m')

This is the single line of code and how it looks in the terminal

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • what version of python are you using? I noticed the syntax changes in 3.6+ here: https://stackoverflow.com/questions/287871/how-to-print-colored-text-in-python – Zorgarath Aug 25 '20 at 06:29
  • I'm using python 3.8, yeah, that Syntax works great in every ocasion (yet) but this one. it seems to not recognize the output of the help() function as the intended text to apply its effects when you put it all in the same line, not really sure why tho – FabioDavidF Aug 25 '20 at 06:40
  • It's possible that the way python performs string interpolation that the formatting happens before the value is inserted, but that's just a guess based on what you are seeing happen. What happens if you allow help to evaluate, capture that value, then use `print(f'\033[34m {theHelpValue} \033[m')`? – Zorgarath Aug 25 '20 at 06:42
  • 1
    Interesting guess, but unfortunately the result is still the same, I tried with this code value = help('for') print(f'\033[41m {value} \033[m') – FabioDavidF Aug 25 '20 at 07:25
  • One last suggestion - what about string concatenation instead of interpolation: `print('\033[34m' + help("for") + '\033[m')` (I don't know python offhand so thats sort of pseudo code) – Zorgarath Aug 25 '20 at 07:29
  • 1
    Still the same, only coloring the "None" text that returns after the result python really doesn't seem to like the idea of coloring the help() function in a compact way hahaha – FabioDavidF Aug 25 '20 at 07:44

1 Answers1

1

The (pretty simple) workaround that I found is to just print the color codes on separate lines

Example:

print('\033[41m')
print(help('for'))
print('\033[m')

That code will make it so the output of the help('for') function has a red background

  • Why `print(help(...))`? `help` writes to stdout on it's own, so it's equivalent to writing `help(...); print(None)`. You can just `help(...)` on its own. – Brian61354270 Mar 29 '23 at 00:57