# Interpolation to any string

**URL:** <https://discourse.julialang.org/t/interpolation-to-any-string/71020>\
**Category:** General Usage\
**Tags:** strings, string-interpolation\
**Created:** [November 5, 2021, 6:34pm UTC](https://discourse.julialang.org/t/interpolation-to-any-string/71020 "2021-11-05T18:34:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [November 5, 2021, 6:34pm UTC](https://discourse.julialang.org/t/interpolation-to-any-string/71020/1 "2021-11-05T18:34:19Z")

</div>

Hi, is it possible to create a universal function to interpolation to any string?

maybe there is a function to pass values to the template one at time?

```julia
teplate = "\$str1 \$str2"

function get_text_by_template(teplate; x...)
     #?
     return text # teplate + x
end

get_text_by_template(teplate; str1 = "Hellow!", str2 = "Need help!") # -> "Hellow! Need help!"

```

---

<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:** [November 5, 2021, 7:47pm UTC](https://discourse.julialang.org/t/interpolation-to-any-string/71020/2 "2021-11-05T19:47:29Z")

</div>

You might want to try some real templating system like [https://github.com/jverzani/Mustache.jl](https://github.com/jverzani/Mustache.jl).

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [November 5, 2021, 8:01pm UTC](https://discourse.julialang.org/t/interpolation-to-any-string/71020/3 "2021-11-05T20:01:04Z")

</div>

There is also `Formatting.jl` if you are not married to the specific format.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 5, 2021, 10:32pm UTC](https://discourse.julialang.org/t/interpolation-to-any-string/71020/4 "2021-11-05T22:32:42Z")

</div>

Possibly `Printf` too:

```julia
using Printf
get_text_by_template(template, x, y) = Printf.format(template, x, y)
template = Printf.Format("%s %s")
str1 = "Hello!"
str2 = "Need help!"
get_text_by_template(template, str1, str2) 
# result:
"Hello! Need help!"

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [November 6, 2021, 6:32am UTC](https://discourse.julialang.org/t/interpolation-to-any-string/71020/5 "2021-11-06T06:32:30Z")

</div>

I wonder why `Printf.format` is neither documented nor exported.

There could easily be

```julia
function printf(io::IO, fmt, args...)
    format(io, fmt, args...)
end

function printf(T::Type, fmt, args...)
    io = StringBuffer()
    printf(io, fmt, args...)
    T(take!(io))
end

printf(io::IO, fmt::AbstractString, args...) = printf(io, Format(fmt), args...)
printf(fmt, args...) = printf(stdout, fmt, args...)

```

and this problem would just be gone.

---

<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 6, 2021, 3:14pm UTC](https://discourse.julialang.org/t/interpolation-to-any-string/71020/6 "2021-11-06T15:14:14Z")

</div>

> [@gustaphe](#):
>
> I wonder why `Printf.format` is neither documented nor exported.

Partly because it’s type unstable and hence relatively slow, I think — in contrast, if you use `@printf` or `@sprintf` then the `Format` object is created at lowering time so it is known to the compiler. So we want to steer people towards the macro interface if possible.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [November 6, 2021, 7:05pm UTC](https://discourse.julialang.org/t/interpolation-to-any-string/71020/7 "2021-11-06T19:05:11Z")

</div>

And how often do you `printf` in a performance critical context? I feel like this could be solved by a mention in the “Performance tips” section, together with `@code_warntype`. Are there no other sub-optimal convenience functions in the language?

Maybe a good midway would be to simplify and document the function, including pointing out the better way to do it, and not exporting it. That way people who run into this can opt in to that loss of performance with `using Printf: printf`.
