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).
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-04-05 14:21:00 +0000
Seen: 5 times
Last updated: Apr 05
Bash: Check at the beginning fo the script if a given Python is active or not
How can I print all but the first 1000 Lines from a File in Bash?
Bash Script that checks if a Host is available or not?
How can I check the expiry of a SSL certificate in the Command Line?
How can I only get the Column with the Filename from a ls -all output with Awk?
What is the Difference of "source script.sh" and "./script.sh" in Bash?
How can I fix error: Security violation, ping RPC from uid 1001 in Slurm log?
I want to grep for "```*" in a File, in other Words: "```" followed by any Word
Sed: I want to remove a Word after all ``` in the given File