How to display text independent of back-end

Hi, I am new to Jupyter.

If I return a plain string from a cell in a Python notebook, it appears in a monospaced font surrounded by single quotes. This doesn’t look very nice.

However, if I return (for example) a Pandas data frame it is shown in a sans font.

As I understand it, the notebook will examine an object to look for the appropriate representation, in order to format it. A string doesn’t have any of these so it just appears ‘plain’

Is there an object I can use to wrap a string so it is formatted? - I know there is IPython.core.display.HTML but this wouldn’t work for a non-HTML back-end, would it?

Note I don’t need any bold, italic … formatting, I just want it to look nicer than the monospace font.

Hope that makes sense. I’m sure this is obvious but I can’t find how to do it.

There’s a lot of Jupyter-centric ways I’d be happy to tell you about; however, I’m not following part of your post:

I’m not following this part of your post. This forum is about Jupyter so you have HTML possibility. What else are you imagining it working in? I guess another way to ask this is if you are also thinking of running the same code some place where it won’t be in a Python kernel-backed Jupyter .ipynb file? Trying to get clarity before I give you details of a few ways to tackle this when running code in a Python-based kernel working with Jupyter.

Thanks for the swift reply!
As I understand it, if I use nbconvert to create, for example, a LaTeX file for publishing that will use a different back-end - and sending HTML to it won’t work.
Link: Using as a command line tool — nbconvert 7.16.4 documentation
Maybe I’ve got this wrong? I think there should be a way to output text that will work for all back-ends.

For example,
HTML back-end gives this:
Screenshot 2024-09-17 135100

LaTeX gives this:

Thanks, that clarifies what you mean better.
I’m not following why you aren’t wrapping using print()? Like print(returned_string)?
Maybe that isn’t supported by LaTeX either?

I know of at least two other ways of doing this that I suspect would work downstream because I think it produces plain text without the quotes, but that is the most common and answers this below:

And if you plan to return a strong or have a returned string as the last expression of a cell a lot & adding print() would be tedious, I’ll mention I think one can write custom code that you place at the top of your notebook and that it would test if you have a string invoked on the last line and if so add the print() behind-the-scenes as the input goes to the kernel so that you don’t have to add it manually every time. I think so. I’d be happy to test and provide code if that would be something you’d be interested in?

If nothing involving print() suffices, we can go the direction of some other suggestions, and so let me know?

OK, thanks. Just plain print seems to work well enough, so I will go with that. For some reason I didn’t think it would work in a notebook, I thought display had to be used instead.

1 Like