简体   繁体   中英

IPython display a string without the quote mark

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)

Try print():

print('Hello world!')

Result:

Hello world!

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!")

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM