1 | initial version |
You can use ANSI escape codes to change the color of the output of a substring of a string in Python when printing to the command line.
Here's an example:
string = "Hello, world!" substring = "world" # Print the string with the substring in green print(string.replace(substring, "\033[32m{}\033[0m".format(substring)))
In this example, the substring "world" is replaced with the ANSI escape code \033[32m (which changes the color to green), followed by the substring itself, and then the reset code \033[0m (which resets the color back to the default).