16

Is it possible to write something like this:

>>> text_output = "Hello World."
>>> print text_output

...where if the text_output is printed to the Python Shell, it is printed in italics?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
user1852430
  • 161
  • 1
  • 1
  • 3

3 Answers3

25

In Python 2 or 3 if your console supports italics, e.g. rxvt-unicode you can precede your Italic text with the ansi escape code for italics \x1B[3m, and then append it with \x1B[0m for reset/normal to return to normal characters.

>>> print("Normal text \x1B[3mitalic text\x1B[0m normal text")

The above code executing and printing italics

rjurney
  • 4,824
  • 5
  • 41
  • 62
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • 1
    Does that mean if I get this: [3mHello World[23m printed, my console does not support italics? :( Drat. – user1852430 Nov 26 '12 at 06:58
  • This prints anything after the `\x1B[23m`. I have updated the answer to reset the characters to standard/reset after the italic text. – rjurney Jun 18 '21 at 23:39
1

Not in the standard Python shell, no. If you want all output text to be in italics, you could use some GUI shell like Dreampie. If you want the ability to actually style text (with italics, bold, etc.), you need to use some GUI library that provides that sort of ability.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
-1

No. The print function outputs to sys.stdout by default. Assuming you're using something like IDLE, this will simply be echoed into IDLE. You can set some options in IDLE itself to show different font faces and sizes and whatnot, but this will modify everything, not just the output of print commands.

Yuushi
  • 25,132
  • 7
  • 63
  • 81
  • What if he's running it in Bash? I think that italics may work there. – Alix Axel Nov 26 '12 at 05:54
  • 1
    @AlixAxel Perhaps. I've honestly never tried it. Apparently it's running on Windows, however, and I'm 99% certain that `cmd` won't cope with italicized text. Powershell, I don't know. – Yuushi Nov 26 '12 at 06:00