# How to use @sprintf with "parametric" format string?

**URL:** https://discourse.julialang.org/t/how-to-use-sprintf-with-parametric-format-string/70364
**Category:** General Usage
**Tags:** formatting, printf
**Created:** [October 26, 2021, 1:10am UTC](https://discourse.julialang.org/t/how-to-use-sprintf-with-parametric-format-string/70364 "2021-10-26T01:10:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ryszard314159](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryszard314159/32/34402_2.png) [@ryszard314159](https://discourse.julialang.org/u/ryszard314159)
#### Post date: [October 26, 2021, 1:10am UTC](https://discourse.julialang.org/t/how-to-use-sprintf-with-parametric-format-string/70364/1 "2021-10-26T01:10:21Z")

</div>

Is there a way to use @sprintf with “parametric” format string? or it has to be hardcoded?  
This is an example:

julia\> @sprintf(“test: %s”, “test”)  
“test: test”  
julia\> fmt = “test: %s”; @sprintf(fmt, “test”)  
ERROR: LoadError: ArgumentError: @sprintf: first argument must be a format string

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 26, 2021, 1:19am UTC](https://discourse.julialang.org/t/how-to-use-sprintf-with-parametric-format-string/70364/2 "2021-10-26T01:19:48Z")

</div>

In Julia 1.6 you can do this:

```julia
julia> import Printf: Format, format

julia> function test(n,x)
           f = Format("%10.$(n)f")
           format(f,x)
       end
test (generic function with 1 method)

julia> test(3,1.0)
" 1.000"

julia> test(5,1.0)
" 1.00000"

```

There is also the `Formatting.jl` package: [https://github.com/JuliaIO/Formatting.jl](https://github.com/JuliaIO/Formatting.jl)

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [April 1, 2024, 4:40pm UTC](https://discourse.julialang.org/t/how-to-use-sprintf-with-parametric-format-string/70364/3 "2024-04-01T16:40:42Z")

</div>

Is there not a solution in which I can simply provide a variable holding the format string? String interpolation does not work either.

---

<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: [April 1, 2024, 8:19pm UTC](https://discourse.julialang.org/t/how-to-use-sprintf-with-parametric-format-string/70364/4 "2024-04-01T20:19:13Z")

</div>

> [@Ahmed\_Salih](#):
>
> Is there not a solution in which I can simply provide a variable holding the format string?

Sure there is:

```julia
julia> fmt = "test: %s"; Printf.format(Printf.Format(fmt), "test")
"test: test"

```

(However, it’s faster if you use a literal format string ala `@sprintf`, since that allows the format to be parsed at compile time. What are you trying to accomplish?)

---

<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: [April 1, 2024, 8:40pm UTC](https://discourse.julialang.org/t/how-to-use-sprintf-with-parametric-format-string/70364/5 "2024-04-01T20:40:32Z")

</div>

Why doesn’t Printf.jl make life easier for users and define such a `printf()` function?

```julia
using Printf
fmt = "test: %.3f"
printf(s, x) = Printf.format(Printf.Format(s), x)
printf(fmt, π) # "test: 3.142"

```

This would also allow:

```julia
n = 2
printf("test2: %.$(n)f", π) # "test2: 3.14"

```

---

<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: [April 1, 2024, 9:01pm UTC](https://discourse.julialang.org/t/how-to-use-sprintf-with-parametric-format-string/70364/6 "2024-04-01T21:01:33Z")

</div>

> [@rafael.guerra](#):
>
> Why doesn’t [Printf.jl](https://juliahub.com/ui/Packages/General/Printf) make life easier for users and define such a `printf()` function?

Probably because you should really be using `@printf` if you can (or the `Printf.format"foo"` string macro), due to the aforementioned compile-time format parsing. A runtime-constructed format string should be a rarity in practice.
