IPython display a string without the quote mark

Viewed 994

Is there a way to use IPyhton display to print a string without printing the quotation marks?

from IPython.display import display

label = display('Hello world!', display_id = True)

The output is:

'Hello world!'

It included the quote marks. I don't want to print it as an HTML like this:

display(HTML('Hello wolrd!'), display_id = True)
4 Answers

display is quite smart, it could display many formats, but by default it uses __repr__ of the object you pass (but you could also define for example _repr_html_, _repr_json_ etc., check this for more information: https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html ).

This is how you could solve your problem:

class printer(str):
    def __repr__(self):
       return self
    
handle = display(printer('Hello world!'), display_id=True)

You could try this:

display(HTML('<pre>Hello wolrd!</pre>'), display_id = True)

You can rewrite String formmater (similar to the example on display docstring with an int):

from IPython.display import display

def str_formatter(string, pp, cycle):
    pp.text(string)

plain = get_ipython().display_formatter.formatters['text/plain']
plain.for_type(str, str_formatter)

display("Hello World!")

Try print():

print('Hello world!')

Result:

Hello world!
Related