# What function is called to convert a type to a string as part of string interpolation?

**URL:** https://discourse.julialang.org/t/what-function-is-called-to-convert-a-type-to-a-string-as-part-of-string-interpolation/123119
**Category:** New to Julia
**Tags:** question
**Created:** [November 26, 2024, 6:16pm UTC](https://discourse.julialang.org/t/what-function-is-called-to-convert-a-type-to-a-string-as-part-of-string-interpolation/123119 "2024-11-26T18:16:35Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)
#### Post date: [November 26, 2024, 6:16pm UTC](https://discourse.julialang.org/t/what-function-is-called-to-convert-a-type-to-a-string-as-part-of-string-interpolation/123119/1 "2024-11-26T18:16:35Z")

</div>

I’m currently using the following code

```julia
example_string = "$(logdata.level)"

```

to convert from a `Base.CoreLogging.LogLevel` to a `String`.

However I would guess that there is a function call which can be used instead to do this?

---

<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: [November 26, 2024, 6:21pm UTC](https://discourse.julialang.org/t/what-function-is-called-to-convert-a-type-to-a-string-as-part-of-string-interpolation/123119/2 "2024-11-26T18:21:12Z")

</div>

Just `string`, but you usually define the called `print` methods instead of `string` methods.

```julia
julia> Meta.@lower "$(logdata.level)"
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = logdata
│ %2 = Base.getproperty(%1, :level)
│ %3 = Base.string(%2)
└── return %3
))))

```

---

<div class="post-metadata">

### Author: ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)
#### Post date: [November 26, 2024, 6:35pm UTC](https://discourse.julialang.org/t/what-function-is-called-to-convert-a-type-to-a-string-as-part-of-string-interpolation/123119/3 "2024-11-26T18:35:31Z")

</div>

But presumably I can’t do

```julia
print(logdata.level)

```

?

So it has to be `String(logdata.level)`? Sorry just slightly confused by your previous comment

---

<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: [November 26, 2024, 6:54pm UTC](https://discourse.julialang.org/t/what-function-is-called-to-convert-a-type-to-a-string-as-part-of-string-interpolation/123119/4 "2024-11-26T18:54:45Z")

</div>

Try in an interactive REPL. `string` and `print`’s docstrings in help mode also explains the `print` methods a bit better. It’ll explain your intuition that instantiating a `String` does not involve the 1-argument `print` to `stdout`.

---

<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: [November 26, 2024, 8:14pm UTC](https://discourse.julialang.org/t/what-function-is-called-to-convert-a-type-to-a-string-as-part-of-string-interpolation/123119/5 "2024-11-26T20:14:58Z")

</div>

> [@world-peace](#):
>
> So it has to be `String(logdata.level)`? Sorry just slightly confused by your previous comment

It’s calling `string(logdata.level)`, which (by default) calls `print` to an `IOBuffer` under the hood (and `print` in turn calls `show` by default).

There is also a `String(x)` constructor, but it is much more limited in what types `x` it supports — basically, just other strings and vectors of bytes, i.e. things which can be “losslessly” represented as a `String`. For example, you can do `string(3)` to get `"3"`, but not `String(3)`.

---

<div class="post-metadata">

### Author: ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)
#### Post date: [November 27, 2024, 7:53am UTC](https://discourse.julialang.org/t/what-function-is-called-to-convert-a-type-to-a-string-as-part-of-string-interpolation/123119/6 "2024-11-27T07:53:56Z")

</div>

Do you happen to know where I can find the documentation for `string` as opposed to `String`? Thanks for the explanation

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [November 27, 2024, 7:56am UTC](https://discourse.julialang.org/t/what-function-is-called-to-convert-a-type-to-a-string-as-part-of-string-interpolation/123119/7 "2024-11-27T07:56:35Z")

</div>

> [@world-peace](#):
>
> Do you happen to know where I can find the documentation for `string` as opposed to `String`?

See the manual:

> **[Strings · The Julia Language](https://docs.julialang.org/en/v1/base/strings/#Base.string)**
>
> Documentation for The Julia Language.

---

<div class="post-metadata">

### Author: ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)
#### Post date: [November 27, 2024, 7:59am UTC](https://discourse.julialang.org/t/what-function-is-called-to-convert-a-type-to-a-string-as-part-of-string-interpolation/123119/8 "2024-11-27T07:59:13Z")

</div>

Excellent!
