# How to use \`display\` in \`sprint\`?

**URL:** <https://discourse.julialang.org/t/how-to-use-display-in-sprint/69040>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 1, 2021, 12:39pm UTC](https://discourse.julialang.org/t/how-to-use-display-in-sprint/69040 "2021-10-01T12:39:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [October 1, 2021, 12:39pm UTC](https://discourse.julialang.org/t/how-to-use-display-in-sprint/69040/1 "2021-10-01T12:39:42Z")

</div>

Hi all 🙂

I want to do something like that to get the display string.

```julia
julia> display_str = sprint(display, Dict(1:10 .=> 1:10))

```

But it does not work. How would I do that?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 1, 2021, 12:57pm UTC](https://discourse.julialang.org/t/how-to-use-display-in-sprint/69040/2 "2021-10-01T12:57:12Z")

</div>

```julia
julia> _io = IOBuffer()

julia> show(_io, "text/plain", Dict(1:10 .=> 1:10))

julia> String(take!(_io))
"Dict{Int64, Int64} with 10 entries:\n 5 => 5\n 4 => 4\n 6 => 6\n 7 => 7\n 2 => 2\n 10 => 10\n 9 => 9\n 8 => 8\n 3 => 3\n 1 => 1"

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 1, 2021, 1:06pm UTC](https://discourse.julialang.org/t/how-to-use-display-in-sprint/69040/3 "2021-10-01T13:06:33Z")

</div>

> [@jling](#):
>
> `show(_io, "text/plain", Dict(1:10 .=> 1:10))`

You could shorten this to `sprint(show, "text/plain", Dict(1:10 .=> 1:10))` to eliminate the explicit `IOBuffer()`, but it is even easier to use `repr`:

```julia
julia> repr("text/plain", Dict(1:10 .=> 1:10))
"Dict{Int64, Int64} with 10 entries:\n 5 => 5\n 4 => 4\n 6 => 6\n 7 => 7\n 2 => 2\n 10 => 10\n 9 => 9\n 8 => 8\n 3 => 3\n 1 => 1"

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 1, 2021, 1:11pm UTC](https://discourse.julialang.org/t/how-to-use-display-in-sprint/69040/4 "2021-10-01T13:11:14Z")

</div>

> [@Impressium](#):
>
> I want to do something like that to get the display string.

Realize, by the way, that `display` doesn’t necessarily print a string at all — for example, it might show an image representation of the object in an environment like Jupyter or Pluto. The whole point of `display` is that it allows the backend to pick which representation of the object to display.

The default `display(x)` used in the REPL correponds to the three-argument `show(stdout, "text/plain", x)` representation. So what you really want is the `text/plain` representation, hence our answers above.

The [manual section on Custom Pretty Printing](https://docs.julialang.org/en/v1/manual/types/#man-custom-pretty-printing) is required reading on this topic, along with the section on [Multimedia I/O](https://docs.julialang.org/en/v1/base/io-network/#Multimedia-I/O).

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [October 1, 2021, 2:42pm UTC](https://discourse.julialang.org/t/how-to-use-display-in-sprint/69040/5 "2021-10-01T14:42:42Z")

</div>

Cool thank you very much. That was really helpful!
