# How to "print" a dataframe representation in a string?

**URL:** https://discourse.julialang.org/t/how-to-print-a-dataframe-representation-in-a-string/28155
**Category:** Data
**Created:** [August 29, 2019, 8:57am UTC](https://discourse.julialang.org/t/how-to-print-a-dataframe-representation-in-a-string/28155 "2019-08-29T08:57:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [August 29, 2019, 8:57am UTC](https://discourse.julialang.org/t/how-to-print-a-dataframe-representation-in-a-string/28155/1 "2019-08-29T08:57:05Z")

</div>

How do I “print” a representation of a dataframe in within a String, e.g. this would not work:

```julia
df = DataFrame(c1=["a","b"],c2=[1,2])
myStr = """
My dataframe:

$(show(df,allrows=true, allcols=true))

bla,bla,bla...
"""

```

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [August 29, 2019, 8:58am UTC](https://discourse.julialang.org/t/how-to-print-a-dataframe-representation-in-a-string/28155/2 "2019-08-29T08:58:51Z")

</div>

You’re looking for `sprint`.

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [August 29, 2019, 9:17am UTC](https://discourse.julialang.org/t/how-to-print-a-dataframe-representation-in-a-string/28155/3 "2019-08-29T09:17:23Z")

</div>

> [@sylvaticus](#):
>
> show(df,allrows=true, allcols=true)

uhhh… can’t get it working (`MethodError`):

```julia
myStr = """
My dataframe:
$(sprint(show,df,allrows=true,allcols=true;context= :compact => true) )
bla,bla,bla...
"""

```

However, just `string(df)` seems to work:

```julia
myStr = """
My dataframe:
$(string(df))
bla,bla,bla...
"""

```

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [August 29, 2019, 9:22am UTC](https://discourse.julialang.org/t/how-to-print-a-dataframe-representation-in-a-string/28155/4 "2019-08-29T09:22:05Z")

</div>

```julia
julia> myStr = """
       My dataframe:
       $(sprint(io -> show(IOContext(io, :compact => true), df, allrows=true, allcols=true)))
       bla,bla,bla...
       """

```

works fine. `string` should be unnessecary when interpolating, since IIRC it will be called automatically.
