# String Interpolation Question

**URL:** https://discourse.julialang.org/t/string-interpolation-question/52752
**Category:** General Usage
**Tags:** strings, string-interpolation
**Created:** [January 3, 2021, 2:51am UTC](https://discourse.julialang.org/t/string-interpolation-question/52752 "2021-01-03T02:51:46Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Bryan\_So](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bryan_so/32/16228_2.png) [@Bryan\_So](https://discourse.julialang.org/u/Bryan_So)
#### Post date: [January 3, 2021, 2:51am UTC](https://discourse.julialang.org/t/string-interpolation-question/52752/1 "2021-01-03T02:51:46Z")

</div>

HI, very simple beginner’s question about string interpolation:

If a variable is a struct that has a Base.string method. Does string interpolation use that method?

This is most curious:

```julia
struct Expression_Symbol
    data::String
end

Base.string(x::Expression_Symbol) = string(x.data)

s = Expression_Symbol("Hello")

println("$s")
println(" $s")

```

Notice the second println has a space in front of $s… The results are drastically different:

```julia
Hello
 Expression_Symbol("Hello")

```

Simply adding a space in front, or after $s the Base.string method will not be called. Why is that?

Very confusing.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [January 3, 2021, 3:07am UTC](https://discourse.julialang.org/t/string-interpolation-question/52752/2 "2021-01-03T03:07:05Z")

</div>

The method you want to overload is actually `Base.show(io::IO, x::Expression_Symbol)` as described here: [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#man-custom-pretty-printing)

In your case, that would be:

```julia
julia> struct Expression_Symbol
           data::String
       end

julia> Base.show(io::IO, x::Expression_Symbol) = print(io, x.data)

julia> s = Expression_Symbol("Hello")
Hello

julia> println("$s")
Hello

julia> println(" $s")
 Hello

```

---

<div class="post-metadata">

### Author: ![Bryan\_So](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bryan_so/32/16228_2.png) [@Bryan\_So](https://discourse.julialang.org/u/Bryan_So)
#### Post date: [January 3, 2021, 3:28am UTC](https://discourse.julialang.org/t/string-interpolation-question/52752/3 "2021-01-03T03:28:47Z")

</div>

I see. Thanks. I knew it’s something simple I have missed.

---

<div class="post-metadata">

### Author: ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)
#### Post date: [January 4, 2021, 5:51pm UTC](https://discourse.julialang.org/t/string-interpolation-question/52752/4 "2021-01-04T17:51:33Z")

</div>

I’d say that the method that should be defined for this use case is not `Base.show`, but `Base.print`:

```julia
julia> struct Expression_Symbol
           data::String
       end

julia> Base.print(io::IO, x::Expression_Symbol) = print(io, x.data)

julia> s = Expression_Symbol("Hello")
Expression_Symbol("Hello")

julia> println("$s")
Hello

julia> println(" $s")
 Hello

```

There is a subtle difference between this and @rdeits’s example. In both cases the conversion of `s` into a string (by the function `string`, via interpolation, etc.) is the same, but look at the line where `s` is created: overloading `Base.show` does not only tell how a string created out of a `Expression_Symbol` will look, but also what string will be used when the object itself is presented.

The recommendation of overloading `print` when you mean to have a particular `string` method for your type can be found in the [documentation of `string` itself](https://docs.julialang.org/en/v1/base/strings/#Base.string):

> `string` should usually not be defined directly. Instead, define a method `print(io::IO, x::MyType)` .

(Likewise, `Base.show` is the function that should be overloaded if what you want is to change the behavior of `repr`.)

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [January 4, 2021, 7:14pm UTC](https://discourse.julialang.org/t/string-interpolation-question/52752/5 "2021-01-04T19:14:42Z")

</div>

You can see what’s going on in the original example with the `Meta.@lower` macro:

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

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

```

The `string` function is called in both cases, but with one argument in the first case and two in the second. Since you have only overloaded the one-argument method of the string function, your method never gets called in the second case. As others have pointed out, one generally does not want to overload `string` at all, but rather `show`.
