擅长:python、mysql、java
<p>也可以在字符串中使用<a href="https://misc.flogisoft.com/bash/tip_colors_and_formatting" rel="nofollow noreferrer">ANSI color codes</a>:</p>
<pre><code># define aliases to the color-codes
red = "\033[31m"
green = "\033[32m"
blue = "\033[34m"
reset = "\033[39m"
t = "That was one hell of a show for a one man band!"
utterances = t.split()
if "one" in utterances:
# figure out the list-indices of occurences of "one"
idxs = [i for i, x in enumerate(utterances) if x == "one"]
# modify the occurences by wrapping them in ANSI sequences
for i in idxs:
utterances[i] = red + utterances[i] + reset
# join the list back into a string and print
utterances = " ".join(utterances)
print(utterances)
</code></pre>