In Python Tutorial in Hindi, We will Learn how print colorful text in Python using TermColor Library.
In this video tutorial going to cover:
- How do you type in color text?
- What Colour is a string in Python?
- How do you change text color in idle Python?
- What are the colors in python?
- How do you make text bold in Python?
Termcolor : ANSII Color formatting for output in terminal.
Termcolor Installation
$ pip install termcolor
Text Properties
Text colors:
- grey
- red
- green
- yellow
- blue
- magenta
- cyan
- white
Text highlights:
- on_grey
- on_red
- on_green
- on_yellow
- on_blue
- on_magenta
- on_cyan
- on_white
Attributes:
- bold
- dark
- underline
- blink
- reverse
- concealed
Terminal properties
Terminal | bold | dark | underline | blink | reverse | concealed |
xterm | yes | no | yes | bold | yes | yes |
linux | yes | yes | bold | yes | yes | no |
rxvt | yes | no | yes | bold/black | yes | no |
dtterm | yes | yes | yes | reverse | yes | yes |
teraterm | reverse | no | yes | rev/red | yes | no |
aixterm | normal | no | yes | no | yes | yes |
PuTTY | color | no | yes | no | yes | no |
Windows | no | no | no | no | yes | no |
Cygwin SSH | yes | no | color | color | color | yes |
Mac Terminal | yes | no | yes | yes | yes | yes |
Examples:
## Termcolor Library: ANSII Color formatting for output in terminal.
# Import Libraries
from termcolor import colored
print("warning")
print("Done")
### Text colors:
warning = colored("warning", "red")
done = colored("done", "green")
print(f"[{warning}] Virus in running")
print(f"[{done}] Virus killed")
### Text highlights:
warning = colored("[warning]", "red", "on_grey")
done = colored("[done]", "green", "on_grey")
print(f"{warning} Virus in running")
print()
print(f"{done} Virus killed")
### Attributes
warning = colored("[warning]", "red", "on_grey", attrs=['reverse'])
done = colored("[done]", "green")
print(f"{warning} Virus in running", end = "\n")
print(f"{done} Virus killed", end="\n")
done = colored("[done]", "green", attrs=['bold'])
print(f"{done} Virus killed")
done = colored("[done]", "green", attrs=['underline'])
print(f"{done} Virus killed")
done = colored("[done]", "green", attrs=['reverse'])
print(f"{done} Virus killed")
######### Downloading Bar
import time
for j in range(1,101):
time.sleep(.02)
downloading = colored("Collecting Data", 'yellow', 'on_grey', attrs=['reverse'])
percentage = colored(f"[{j}%]", 'blue')
bar = colored('|' * j, "green")
color = downloading + percentage + bar
print(color, end="\r")
print("\n", end="\n")
for j in range(1,101):
time.sleep(.2)
downloading = colored("Downloading", 'red', 'on_grey', attrs=['reverse'])
percentage = colored(f"[{j}%]", 'blue')
bar = colored('|' * j, "green")
color = downloading + percentage + bar
print(color, end="\r")
REF: https://pypi.org/project/termcolor/
Pingback: How to Print Colorful Text In Python | Python Tutorial | Termcolor - The Small World