Here’s a simple example:
from rich import print print("[bold red]alert![/bold red] Something happened")
If you don’t close a style, it will apply until the end of the string. Which is sometimes convenient if you want to style a single line. For example:
print("[bold italic yellow on red blink]This text is impossible to read")
There is a shorthand for closing a style. If you omit the style name from the closing tag, Rich will close the last style. For example:
print("[bold red]Bold and red[/] not bold or red")
Occasionally you may want to print something that Rich would interpret as markup. You can escape square brackets by doubling them up. Here’s an example:
>>> from rich import print >>> print("foo[[bar]]") foo[bar]
escape() will handle escape of text for you.
Be careful when using f-strings with console markup. You will need to escape any variables if they could contain square brackets.
Console markup is convenient, but you may wish to disable it if the syntax clashes with the string you want to print. You can do this by setting
markup=False on the
print() method or on the