1 day ago · Tech · hide · 0 comments

Terminal colors are determined by ANSI escape codes, which are just strings of text. The ANSI escape code for a red color is \033[31m. If we prepend that to a string that we print out to the terminal, the string comes out red. This works regardless of the programming language. Here’s an example in Python: print("\033[31mthis text is red")We can split out the ANSI escape code to make it easier to see the two parts: red_ansi_code = "\033[31m" message = "this text is red" print(red_ansi_code + message)Both code snippets above produce the following output: this text is red We can mix colors too. Here is an example with green and blue as well as red. We use the escape code \033[0m to terminate the colorization of text after each string. print("\033[31mred text\033[0m \033[32mgreen text\033[0m \033[34mblue text\033[0m")We can split things out once again to make it more legible: red_ansi_code = "\033[31m" green_ansi_code = "\033[32m" blue_ansi_code = "\033[34m" terminator = "\033[0m" print(…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.