How to print italic text in f-strings in Jupyter Notebook

1.5k Views Asked by At

I am trying to use f-strings to print some parts of text in italic format.

Browsing the web for "python"+"print"+"italic"+"f-strings", I found these ANSI codes should do the work : '\x1B[3m' and '\x1B[0m' ; although they don't :

print(f'\x1B[3m italic \x1B[0m' + f'not italic') 

gives no italic output :

italic not italic

Am I missing some update? My python version is 3.9.7 and I'm using Jupyter Notebook.

2

There are 2 best solutions below

2
ljmc On

ANSI escape work with python or ipython, but only in the terminal.

terminal screenshot

0
ovhaag On

In Jupyter Notebooks, you can use

from IPython.display import Markdown, display
display(Markdown("normal *italic* normal"))

See printing bold, colored, etc., text in ipython qtconsole, https://stackoverflow.com/a/46934204/747202