0

I want to know how to do a print() or a os.system() with mutiple colors in one function. Exemple:

import requests

requests.put(LINK)

print(f"{MutipleColorsHere} {r.json()['response']}")

You can see more with this picture

screenshot

the image is the output of the requests.

I only want explanations/examples.

More Information:

If I get multiple responses, at each new line, they will be in a new colour.

martineau
  • 119,623
  • 25
  • 170
  • 301
zzeua
  • 15
  • 1
  • 1
  • 5
  • 1
    Does this answer your question? [How do I print colored text to the terminal?](https://stackoverflow.com/questions/287871/how-do-i-print-colored-text-to-the-terminal) – SuperStormer May 21 '22 at 20:49
  • I know how to print colored text to the terminal. I want different colors at each response on the terminal. `{F.RED} Response {F.ORANGE} Response {F.YELLOW} Response` like this ^^ but in one single print function. If that makes sense. – zzeua May 21 '22 at 20:55
  • @SuperStormer Like [this](https://kidscodecs.com/python-rainbow-colors/) But with a single color & a new line. – zzeua May 21 '22 at 21:00
  • You can do something similar to what is shown in your question, but not with that many different colors because terminals only supports a relatively small number of them. – martineau May 21 '22 at 21:32
  • I don't want that many colors. I just want to know how to do them. It would help me in the future. – zzeua May 21 '22 at 21:40
  • You need to inject the color escapes at the appropriate point in the strings (after each newline, for example). There isn't a single code you can put before a multiline string that will do that for you. – chepner May 21 '22 at 21:47
  • @chepner so `print(f"{Fore.RED} str\n{Fore.BLUE} $$$")`? – zzeua May 21 '22 at 22:33
  • Right. You'd have to split a multiline string yourself to get a list of strings (each to be colored differently), but at that point, there's no compelling reason to create one single string from your colors and the list, rather than just using a bunch of print statements or a loop. – chepner May 21 '22 at 22:56
  • So, I need to do this `print(f"{Fore.RED} str\n{Fore.BLUE} $$$")` but with many more in a multiline string? Exemple: `print(f"""{Fore.RED} str\n{Fore.BLUE} $$$\n{Fore.GREEN} green colored world""")`. – zzeua May 22 '22 at 00:20
  • zzeua: Yes, it will need to be done as many times are there are color changes. Making the string multiline is at least partially why it seems like there are so many. – martineau May 30 '22 at 00:47

0 Answers0