# String interpolation formatting

**URL:** https://discourse.julialang.org/t/string-interpolation-formatting/91454
**Category:** General Usage
**Tags:** string-interpolation
**Created:** [December 9, 2022, 4:05am UTC](https://discourse.julialang.org/t/string-interpolation-formatting/91454 "2022-12-09T04:05:06Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)
#### Post date: [December 9, 2022, 4:05am UTC](https://discourse.julialang.org/t/string-interpolation-formatting/91454/1 "2022-12-09T04:05:06Z")

</div>

Consider

```julia
println("The square root of pi is $(sqrt(pi))")

```

which displays

```julia
The square root of pi is 1.7724538509055159

```

How can I change this code if I only want to print the answer to (say) two decimal places?

I know that there exist packages that provide macros like `@sprintf`, but that seems complicated and inconvenient for such a simple task.

I would expect this sort of thing to be built into the string interpolation syntax like it is in Python, so mostly I want to check that it isn’t.

---

<div class="post-metadata">

### Author: ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)
#### Post date: [December 9, 2022, 4:15am UTC](https://discourse.julialang.org/t/string-interpolation-formatting/91454/2 "2022-12-09T04:15:35Z")

</div>

```julia
println("The square root of pi is $(round(sqrt(pi),digits=2))")

```

---

<div class="post-metadata">

### Author: ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)
#### Post date: [December 9, 2022, 9:57am UTC](https://discourse.julialang.org/t/string-interpolation-formatting/91454/3 "2022-12-09T09:57:09Z")

</div>

That works as a hack, but how would you do zero padding, for example?

---

<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: [December 9, 2022, 10:06am UTC](https://discourse.julialang.org/t/string-interpolation-formatting/91454/4 "2022-12-09T10:06:29Z")

</div>

> [@Nathaniel](#):
>
> but that seems complicated and inconvenient for such a simple task.

Agree to disagree:

```julia
using Printf
println("The square root of pi is $(@sprintf("%07.2f", √π))")

```

Actually better written as:

```julia
@printf("The square root of pi is %07.2f\n", √π)

```

---

<div class="post-metadata">

### Author: ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)
#### Post date: [December 9, 2022, 10:06am UTC](https://discourse.julialang.org/t/string-interpolation-formatting/91454/5 "2022-12-09T10:06:41Z")

</div>

> I know that there exist packages that provide macros like `@sprintf`, but that seems complicated and inconvenient for such a simple task.

Well, that package is in the stdlib.

[https://docs.julialang.org/en/v1/stdlib/Printf/#man-printf](https://docs.julialang.org/en/v1/stdlib/Printf/#man-printf)

---

<div class="post-metadata">

### Author: ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)
#### Post date: [December 10, 2022, 2:14pm UTC](https://discourse.julialang.org/t/string-interpolation-formatting/91454/6 "2022-12-10T14:14:15Z")

</div>

but … the whole point of string interpolation is not to have to use the inconvenient printf syntax where the variables appear at the end instead of at the place where they’re used. So for me the @sprintf version is much better than the @printf one, but it absolutely isn’t readable or convenient to type.

---

<div class="post-metadata">

### Author: ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)
#### Post date: [December 10, 2022, 4:06pm UTC](https://discourse.julialang.org/t/string-interpolation-formatting/91454/7 "2022-12-10T16:06:37Z")

</div>

Is [PyFormattedStrings](https://juliahub.com/ui/Packages/PyFormattedStrings/T968L/0.1.10) closer to what you’re looking for?

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [November 21, 2023, 3:07am UTC](https://discourse.julialang.org/t/string-interpolation-formatting/91454/8 "2023-11-21T03:07:57Z")

</div>

Another solution would be to use [StrFormat](https://github.com/JuliaString/StrFormat.jl), and you can write it as:  
`println(f"The square root of pi is \%07.2f(√π)")`  
or (a short form, useful for when you are inserting debugging print statements)  
`pr"The square root of pi is \%07.2f(√π)"`

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [November 21, 2023, 3:16am UTC](https://discourse.julialang.org/t/string-interpolation-formatting/91454/9 "2023-11-21T03:16:23Z")

</div>

I’d probably avoid the JuliaString packages for now until they’ve undergone more rigorous testing.

> <https://github.com/JuliaString/Format.jl/issues/63>
>
> It seems that this port is bringing over the following rather serious correctnes…s bug from \`Formatting.jl\`:
> 
> https://github.com/JuliaIO/Formatting.jl/issues/108
> 
> \`\`\`
> julia\> using Format
> julia\> fspec = FormatSpec(".1e")
> julia\> printfmt(fspec, 0.0003)
> 2.10e-04
> \`\`\`

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [November 21, 2023, 3:25am UTC](https://discourse.julialang.org/t/string-interpolation-formatting/91454/10 "2023-11-21T03:25:52Z")

</div>

That bug you are referring to is one I inherited from the Formatting.jl package - and it only affects the use of the Python style `printfmt` and `FormatSpec` interface - not the code used by the C printf style formatting used by StrFormat (which is pretty much identical to the formatting code in stdlib/Printf.jl)  
The JuliaString packages have been fairly rigorously tested (in many cases there’s a lot more testing of the string functions than in base for `String`), although it’s true I hadn’t done much to write additional tests for the Python style formatting from the JuliaIO/Formatting.jl package (not being a Python programmer myself)

---

<div class="post-metadata">

### Author: ![tverho](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tverho](https://discourse.julialang.org/u/tverho)
#### Post date: [November 21, 2023, 8:24am UTC](https://discourse.julialang.org/t/string-interpolation-formatting/91454/11 "2023-11-21T08:24:18Z")

</div>

There’s also [GitHub - magonser/FStrings.jl](https://github.com/magonser/FStrings.jl) to do the same. It has a nicer name but I don’t know how it compares in features.

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [November 21, 2023, 1:53pm UTC](https://discourse.julialang.org/t/string-interpolation-formatting/91454/12 "2023-11-21T13:53:07Z")

</div>

I’m not sure if that’s being maintained - the last change was from 2 years ago

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [November 21, 2023, 3:00pm UTC](https://discourse.julialang.org/t/string-interpolation-formatting/91454/13 "2023-11-21T15:00:03Z")

</div>

That seems like a reason to also not use JuliaIO/Formatting.jl, rather than a reason to use Format.jl. It seems the Printf stdlib does not have such egregious bugs (/ when it does, they are fixed) and so it should be preferred over both.

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [February 15, 2024, 7:52pm UTC](https://discourse.julialang.org/t/string-interpolation-formatting/91454/14 "2024-02-15T19:52:47Z")

</div>

All of the bugs that had been found in [JuliaIO/Formatting.jl](https://github.com/JuliaIO/Formatting.jl) have now been fixed in [JuliaString/Format.jl](https://github.com/JuliaString/Format.jl), as well as a number features in Python formatted strings have now been implemented as well (such as center justification, with ^, using , or \_ to group digits, and precision to truncate strings).

Note that Printf does have some bugs handling precision to truncate strings (in particular, when there are wide characters in the string)
