# Any significant difference between string, \*, and interpolation for concatenation?

**URL:** <https://discourse.julialang.org/t/any-significant-difference-between-string-and-interpolation-for-concatenation/46977>\
**Category:** New to Julia\
**Created:** [September 21, 2020, 3:13am UTC](https://discourse.julialang.org/t/any-significant-difference-between-string-and-interpolation-for-concatenation/46977 "2020-09-21T03:13:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [September 21, 2020, 3:13am UTC](https://discourse.julialang.org/t/any-significant-difference-between-string-and-interpolation-for-concatenation/46977/1 "2020-09-21T03:13:00Z")

</div>

I’m writing some training material, and I was wondering if there is any difference between the three methods for concatenation that I should be aware of?

The most easiest way to do it is:

```julia
greet = "Hello"
whom = "World"
println("$greet, $whom")

```

For new comers to Julia, is teaching the `string()` and `*` useful?

It seems if you’re putting 2 or 3 strings together using `string()` and `*` it shouldn’t be a problem. It may become difficult when you have a lot of variables, and need to piece together strings, punctuation and `\n`.

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [September 21, 2020, 5:20am UTC](https://discourse.julialang.org/t/any-significant-difference-between-string-and-interpolation-for-concatenation/46977/2 "2020-09-21T05:20:13Z")

</div>

The interpolation gets rewritten internally as `string()` so that should be the same. `string()` will also parse other data types as strings if necessary.

[https://docs.julialang.org/en/v1/manual/strings/#man-concatenation](https://docs.julialang.org/en/v1/manual/strings/#man-concatenation)

---

<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:** [September 21, 2020, 5:27am UTC](https://discourse.julialang.org/t/any-significant-difference-between-string-and-interpolation-for-concatenation/46977/3 "2020-09-21T05:27:32Z")

</div>

If I want to print several variables at the same time, I usually use the long form of `println(a, b, c, ...)`. If I have to build up a really long string from a large number of values in a loop, I usually use an `IOBuffer` to print into, since that will create the string object itself only when it’s consumed. Because strings are immutable in julia, each application of `*` has to create a new string and can’t modify existing strings, so for really long string concatenation, an IOBuffer is usally faster. This is advanced though and isn’t particularly helpful in introductory material - `*`, `string` and multi-argument `println` should be fine there.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 21, 2020, 6:04am UTC](https://discourse.julialang.org/t/any-significant-difference-between-string-and-interpolation-for-concatenation/46977/4 "2020-09-21T06:04:25Z")

</div>

> [@anon60034542](#):
>
> For new comers to Julia, is teaching the `string()` and `*` useful?

Possibly. Depends on the focus of the course, and whether they need text manipulation. For simple error messages interpolation should be fine, for delimited joining there is also `join`.

---

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [September 21, 2020, 2:06pm UTC](https://discourse.julialang.org/t/any-significant-difference-between-string-and-interpolation-for-concatenation/46977/5 "2020-09-21T14:06:24Z")

</div>

On the surface using `$` looks straightforward, especially when you have to deal with quoting punctuation, and `".\n"`.

A beginner might want to know why they should learn `string()` and `*` when `$` looks easier? I’ll admit, I’m not too sure myself, so I was wondering what the difference is between them. Does `string()` or `*` provide some functionality that `$` doesn’t?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 21, 2020, 2:22pm UTC](https://discourse.julialang.org/t/any-significant-difference-between-string-and-interpolation-for-concatenation/46977/6 "2020-09-21T14:22:34Z")

</div>

Not really, it’s just different syntax for the same thing. Cf

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

```

Different syntax is occasionally useful — sometimes one alternative is more convenient, sometimes the other. Personally I find `$` interpolations quite nice for info/error messages that can be read as a template. Just use whatever you prefer.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [September 21, 2020, 3:01pm UTC](https://discourse.julialang.org/t/any-significant-difference-between-string-and-interpolation-for-concatenation/46977/7 "2020-09-21T15:01:11Z")

</div>

For completeness, the `*` function also calls `string` internally for concatination, see

[https://github.com/JuliaLang/julia/blob/697e782ab86bfcdd7fd15550241fe162c51d9f98/base/strings/basic.jl#L251](https://github.com/JuliaLang/julia/blob/697e782ab86bfcdd7fd15550241fe162c51d9f98/base/strings/basic.jl#L251)
