简体   繁体   中英

How do I color a string in python terminal?

I am trying to color only a single variable in Python 3, however whenever I run the code, the sring gets colored in my terminal, but then everything else after that is colored too. I am trying to do it without any imports. Here's my code.

tentativas = 20
print(f'Tentativas restantes:', f'\033[0;36m {tentativas}')

To reset the coloring you have to use "\033[0;0m" , either set it up as a prefix in your next print eg

print("\033[0;0m This is your next print")

Or perform an empty print just to reset the color

print("\033[0;0m", end='')

You can use the module colorama

Install: pip install colorama

Open script
from colorama import Force as color

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM