# Is there a direct way to get the string of the output of \`show\`?

**URL:** https://discourse.julialang.org/t/is-there-a-direct-way-to-get-the-string-of-the-output-of-show/116188
**Category:** General Usage
**Tags:** question, show, print
**Created:** [June 25, 2024, 7:29am UTC](https://discourse.julialang.org/t/is-there-a-direct-way-to-get-the-string-of-the-output-of-show/116188 "2024-06-25T07:29:22Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)
#### Post date: [June 25, 2024, 7:29am UTC](https://discourse.julialang.org/t/is-there-a-direct-way-to-get-the-string-of-the-output-of-show/116188/1 "2024-06-25T07:29:22Z")

</div>

I need to write the following script:

```julia
# typeof
# 値の型を得る

function print_typeof(x)
    print("表現「")
    show(x)
    println("」の値の型は「", typeof(x), "」である。")
end

print_typeof(true)
print_typeof(1)
print_typeof(1.0)
print_typeof('1')
print_typeof("1")

```

to get the following result:

```julia
表現「true」の値の型は「Bool」である。
表現「1」の値の型は「Int64」である。
表現「1.0」の値の型は「Float64」である。
表現「'1'」の値の型は「Char」である。
表現「"1"」の値の型は「String」である。

```

I want to write it more simple but `show` doesn’t accept multiple arguments.

```julia
# impossible
show(a, b, c, d, e, f, g)
# possible
print(a, b, c, d, e, f, g)

```

So I need to define the following function to use print.

```julia
function showstring(x)
    io = IOBuffer()
    show(io, x)
    result = String(take!(io))
    close(io)
    result
end

print(a, b, showstring(c), d, e, showstring(g))

```

By this, I can write `print_typeof` as

```julia
print_typeof(x) = println("表現「", showstring(x), "」の値の型は「", typeof(x), "」である。")

```

or

```julia
sentence_typeof(x) = "表現「$(showstring(x))」の値の型は「$(typeof(x))」である。"
const print_typeof = println ∘ sentence_typeof

```

Is there another way to shorten the definition of my `print_typeof` or `showstring`?

Or, should I define `showmultiple`?

```julia
showmultiple(xs...) = foreach(show, xs)

```

---

<div class="post-metadata">

### Author: ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)
#### Post date: [June 25, 2024, 7:33am UTC](https://discourse.julialang.org/t/is-there-a-direct-way-to-get-the-string-of-the-output-of-show/116188/2 "2024-06-25T07:33:46Z")

</div>

I think you can replace `showstring(x)` by `sprint(show, x)`.

---

<div class="post-metadata">

### Author: ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)
#### Post date: [June 25, 2024, 7:54am UTC](https://discourse.julialang.org/t/is-there-a-direct-way-to-get-the-string-of-the-output-of-show/116188/3 "2024-06-25T07:54:47Z")

</div>

`repr(x)` is the same as `sprint(show, x)`

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [June 25, 2024, 8:06am UTC](https://discourse.julialang.org/t/is-there-a-direct-way-to-get-the-string-of-the-output-of-show/116188/4 "2024-06-25T08:06:04Z")

</div>

I’m confused about many things here.

1. You say `show` doesn’t accept multiple arguments, but that’s what `print`/`println` is for.

2. Why does `print_typeof` have a separate `show` step? `print`/`println` calls `show` internally, so `println("表現「", x, "」の値の型は「", typeof(x), "」である。")` does the same thing AFAIK. Concatenation and string interpolation call `string` internally, which also internally calls `print` and thus `show`, so `"表現「$x」の値の型は「$(typeof(x))」である。"` also does the same thing. `print` only deviates from `show` for formatting features, like removing the quotes from printed strings.

3. Why make `showstring` when the docstring for `show` already says `repr` gives its output as a `String`?

---

<div class="post-metadata">

### Author: ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)
#### Post date: [June 25, 2024, 8:07am UTC](https://discourse.julialang.org/t/is-there-a-direct-way-to-get-the-string-of-the-output-of-show/116188/5 "2024-06-25T08:07:16Z")

</div>

For 2., `print("hello")` and `show("hello")` give different results. It is necessary to distinguish between them for my purpose.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [June 25, 2024, 8:07am UTC](https://discourse.julialang.org/t/is-there-a-direct-way-to-get-the-string-of-the-output-of-show/116188/6 "2024-06-25T08:07:47Z")

</div>

That’s what the last sentence of 2. means

---

<div class="post-metadata">

### Author: ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)
#### Post date: [June 25, 2024, 8:11am UTC](https://discourse.julialang.org/t/is-there-a-direct-way-to-get-the-string-of-the-output-of-show/116188/7 "2024-06-25T08:11:01Z")

</div>

For 3., I overlooked that the `show` docstring mentions `repr` and I haven’t used it.

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [June 25, 2024, 8:50am UTC](https://discourse.julialang.org/t/is-there-a-direct-way-to-get-the-string-of-the-output-of-show/116188/8 "2024-06-25T08:50:47Z")

</div>

I suppose that you want following functionality provided most easily by the `IOCapture` package:

```julia
using IOCapture
function print_typeof(x)
	c = IOCapture.capture() do; show(x) end
	println("表現「", c.output, "」の値の型は「", typeof(x), "」である。")
end

```

Alternatively you can use the `Suppressor` package:

```julia
#using IOCapture
using Suppressor
function print_typeof(x)
	#c = IOCapture.capture() do; show(x) end
	#println("表現「", c.output, "」の値の型は「", typeof(x), "」である。")
	strOfStdOut = @capture_out show(x)
	println("表現「", strOfStdOut, "」の値の型は「", typeof(x), "」である。")
end

```

For the sake of completeness below an approach ( mentioned by [oxinabox](https://discourse.julialang.org/u/oxinabox) in post #3 ) which in the simple case of a `show()` function delivers the required result without the need of usage of any package:

```julia
#using IOCapture
#using Suppressor
function print_typeof(x)
	#c = IOCapture.capture() do; show(x) end
	#println("表現「", c.output, "」の値の型は「", typeof(x), "」である。")
	#strOfStdOut = @capture_out show(x)
	typeOf = typeof(x)
	#println("reprOfValue「$strOfStdOut」typeOfValue 「$typeOf」")
	reprOf = repr(x)
	println("reprOfValue「$reprOf」typeOfValue 「$typeOf」")

end

```

---

<div class="post-metadata">

### Author: ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)
#### Post date: [June 25, 2024, 9:23am UTC](https://discourse.julialang.org/t/is-there-a-direct-way-to-get-the-string-of-the-output-of-show/116188/9 "2024-06-25T09:23:55Z")

</div>

Indeed, it seems that these packages and methods can achieve the goal, but they are overkill for my purposes. In any case, thank you for telling me methods I didn’t know about.
