# Formatted string interpolation

**URL:** <https://discourse.julialang.org/t/formatted-string-interpolation/21350>\
**Category:** New to Julia\
**Tags:** string-interpolation\
**Created:** [March 1, 2019, 3:29pm UTC](https://discourse.julialang.org/t/formatted-string-interpolation/21350 "2019-03-01T15:29:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [March 1, 2019, 3:29pm UTC](https://discourse.julialang.org/t/formatted-string-interpolation/21350/1 "2019-03-01T15:29:31Z")

</div>

I’m currently looking for a package providing format options when interpolating values in a string, like e.g. in python: `f'abc {x:.2f} def'`. `StringLiterals` from JuliaString ([GitHub - JuliaString/StringLiterals.jl: Implement improved string literals with Swift-style syntax for interpolation, hex, & unicode characters, plus C & Python style formatting and Unicode, HTML, LaTeX, and Emoji entities](https://github.com/JuliaString/StringLiterals.jl)) does exactly what I need, but it does not work on Julia 1+. I would be very surprised if there is indeed no possibility for formatted interpolation, so probably I just could not find the package to do this. Could you please help and point in the right direction?

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [March 1, 2019, 3:42pm UTC](https://discourse.julialang.org/t/formatted-string-interpolation/21350/2 "2019-03-01T15:42:00Z")

</div>

Does this work for you?

```julia
using Printf
@sprintf "abc %.3f def" pi

```

there is also the more general interpolation with `$`:

```julia
"abc $(format_my_variable(x)) def"

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [March 1, 2019, 4:03pm UTC](https://discourse.julialang.org/t/formatted-string-interpolation/21350/3 "2019-03-01T16:03:18Z")

</div>

`sprintf` is definitely not interpolation - just imagine replacing something similar to `'a = {xyz:.4f} | b = {abs(var):05d} | ...'` with more variables and longer names. It is very confusing and error-prone to use `sprintf` here, especially when modifying a line like this: easy to insert a new variable into wrong place, or remove the wrong one. With interpolation it is much much better (and I guess this is one of the main reasons to have interpolation in the first place). Of course, `"a = $(@sprintf "%.4f" xyz) | b = $(@sprintf "%05d" abs(var)) | ..."` will work, but is so verbose for such a common task.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [March 1, 2019, 4:04pm UTC](https://discourse.julialang.org/t/formatted-string-interpolation/21350/4 "2019-03-01T16:04:59Z")

</div>

Try this:

> **[GitHub - JuliaIO/Formatting.jl: A Julia package to provide Python-like...](https://github.com/JuliaIO/Formatting.jl)**
>
> A Julia package to provide Python-like formatting support - GitHub - JuliaIO/Formatting.jl: A Julia package to provide Python-like formatting support

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [March 1, 2019, 4:07pm UTC](https://discourse.julialang.org/t/formatted-string-interpolation/21350/5 "2019-03-01T16:07:26Z")

</div>

I looked at `Formatting.jl`, but didn’t find interpolation there. This package looks like `sprintf` with a slightly different syntax - so one still needs to keep track of the variables order.

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [December 5, 2019, 11:12pm UTC](https://discourse.julialang.org/t/formatted-string-interpolation/21350/6 "2019-12-05T23:12:01Z")

</div>

I just submitted a [PR](https://github.com/JuliaString/StringLiterals.jl/pull/12) to `StringLiterals.jl` that makes it working under Julia 1+ 🙂

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [September 7, 2020, 7:11am UTC](https://discourse.julialang.org/t/formatted-string-interpolation/21350/7 "2020-09-07T07:11:47Z")

</div>

Just saw that someone liked my post. Please read the above-mentioned link. `StringLiterals.jl` has been superseeded by StrLiterals (+ `StrFormat.jl` & `StrEntities.jl`)  
But the PR also made it to StrLiterals 🙂
