# How to repeat the format in @printf?

**URL:** <https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695>\
**Category:** General Usage\
**Tags:** io\
**Created:** [August 19, 2021, 11:48pm UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695 "2021-08-19T23:48:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 19, 2021, 11:48pm UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/1 "2021-08-19T23:48:59Z")

</div>

Sorry for another stupid question. Not proud of asking these kind questions 😅.  
I am sure it is stupid, however I searched google and stack overflow but still not sure how to do this.

The question is very simply,

```
using Printf
@printf("%9.3f %9.3f %9.3f %9.3f",1.0,2.0,3.0,4.0)

```

I need to display 4 numbers from 1.0 to 4.0 for example. Now, do I have to manually put %9.3f for four times in the fmt?

In Fortran I can do things like

```
4(9.3f)

```

So that I do not need to put 9.3f 4 times.

In Julia, or in printf, is there a similar way so that I do not need to repeat 9.3f 4 times?

Thank you very much in advance!

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [August 20, 2021, 12:11am UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/2 "2021-08-20T00:11:18Z")

</div>

In Julia 1.6 you can use `Printf.Format`, and then `Printf.format` instead of the macro:

```julia-repl
julia> f = Printf.Format("%9.3f "^4);

julia> Printf.format(f, 1,2,3,4)
" 1.000 2.000 3.000 4.000 "

julia> Printf.format(stdout, f, 1,2,3,4)
    1.000 2.000 3.000 4.000 

```

(Well, there is an unwanted trailing space in this example, but this can be easily worked around with other ways of making the format string.)

---

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 20, 2021, 12:16am UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/3 "2021-08-20T00:16:39Z")

</div>

Thank you so much! That is so cool!  
Nice, this should be way to go!  
This is similar with what is in Fortran, like, I can define a format first (with variable in it), like

```
write(fmt_muk_iter, '(a, i0, a)') '(''Mu_k ='',t20,',kmixin,'( f12.5,'' +-'',f12.5,1x,''|'' ))' 

```

Then I can use the format fmt\_muk\_iter that I defined to do output

```
write(6,fmt_muk_iter) (valmusigma(k,2),errormusigma(k,2),k=1,kmix)   

```

What you said looks similar!

---

<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:** [August 20, 2021, 12:25am UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/4 "2021-08-20T00:25:21Z")

</div>

The `@sprintf` macro produces a string (instead of printing it), and then you can use something like this:

```julia
julia> using Printf

julia> x = rand(5);

julia> println([@sprintf(" %5.2f",x) for x in x]...)
  0.22 0.51 0.82 0.49 0.90

```

I think this is less “efficient”, but for printing a bunch of tables in non-critical code that’s practical.

---

<div class="post-metadata">

**Author:** ![rashid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rashid/32/9398_2.png) [@rashid](https://discourse.julialang.org/u/rashid)\
**Post date:** [August 20, 2021, 2:30am UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/5 "2021-08-20T02:30:34Z")

</div>

A workaround I used to use for this before discovering the solution suggested by @heliosdrm. This is probably not suggested, but is very convenient for code interpolation in macros.

```julia
julia> using Printf

julia> @eval @printf($("%9.3f "^4), 1.0, 2.0, 3.0, 4.0)
    1.000 2.000 3.000 4.000 

```

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [August 20, 2021, 8:18am UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/6 "2021-08-20T08:18:52Z")

</div>

The disadvantage of using `@eval` is that it evaluates the expression in the global scope, so it’s not great for using in functions etc.

---

<div class="post-metadata">

**Author:** ![rashid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rashid/32/9398_2.png) [@rashid](https://discourse.julialang.org/u/rashid)\
**Post date:** [August 20, 2021, 8:40am UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/7 "2021-08-20T08:40:07Z")

</div>

Yes, that’s true. It would be nice to have a macro which achieves similar functionality, i.e just evaluates only the code inside `$(...)`. I don’t think that would be hard. Atleast for me, I have needed to use `@eval` for this purpose frequently for evaluating some code which I need to pass to another macro. But I haven’t seen this proposed anywhere.

---

<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:** [August 20, 2021, 12:44pm UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/8 "2021-08-20T12:44:12Z")

</div>

@heliosdrm, that is really nice.  
We can then even broadcast `Printf` to arrays using your solution:

```julia
v = rand(10)
f = Ref(Printf.Format("%5.2f"));
Printf.format.(f,v)

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [August 20, 2021, 1:39pm UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/9 "2021-08-20T13:39:20Z")

</div>

Also, simply `map`:

```julia
map(x->@printf("%9.3f ",x), (1.0,2.0,3.0,4.0));
    1.000 2.000 3.000 4.000

```

---

<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:** [August 20, 2021, 10:35pm UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/10 "2021-08-20T22:35:15Z")

</div>

One more way with piping:

```julia
(1.0,2.0,3.0,4.0).|>(x->@sprintf("%9.3f",x))|>join
" 1.000 2.000 3.000 4.000"

```

---

<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:** [August 20, 2021, 11:37pm UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/11 "2021-08-20T23:37:42Z")

</div>

I like this way as well:

```julia
julia> using Printf

julia> myformat(x) = @sprintf("%5.2f",x)
myformat (generic function with 1 method)

julia> x = rand(3);

julia> println(myformat.(x)...)
 0.15 0.68 0.58

```

---

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 21, 2021, 6:45am UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/12 "2021-08-21T06:45:12Z")

</div>

A quick thing, is it possible to print these stuff in a txt file?

I tried things like

```
f = Printf.Format("%9.3f"^4)
io = open("myfile.txt", "w")
for i in 1:10
    @printf(io, f, 1,2,3,4)
end
close(io)

```

But it does not work.

I expect to have a file called myfile.txt in which it should have such 5 lines,

```
1.000 2.000 3.000 4.000
1.000 2.000 3.000 4.000
1.000 2.000 3.000 4.000
1.000 2.000 3.000 4.000
1.000 2.000 3.000 4.000

```

Thank you!

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [August 21, 2021, 7:47am UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/13 "2021-08-21T07:47:22Z")

</div>

Note that if you are writing tabular data you can just use CSV.jl and Dataframes. Writing and reading text files can be quite complicated (lots of corner cases) so it’s better to use a library that is well tested.

---

<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:** [August 21, 2021, 8:21am UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/14 "2021-08-21T08:21:02Z")

</div>

@CRquantum, you could write like this (_added carriage return for better formatting_):

```julia
f = Printf.Format("%9.3f"^4*"\n")
io = open("myfile2.txt", "w")
for i in 1:10
    Printf.format(io, f, 1,2,3,4)
end
close(io)

```

---

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 24, 2021, 3:40am UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/15 "2021-08-24T03:40:47Z")

</div>

> [@rafael.guerra](#):
>
> ```julia
> f = Printf.Format("%9.3f"^4*"\n")
> io = open("myfile2.txt", "w")
> for i in 1:10
> Printf.format(io, f, 1,2,3,4)
> end
> close(io)
> 
> ```

Thank you very much!

One more question, in the exactly the same example in the quote, if I put 1, 2, 3, 4 as an array a

```
a = [1.0, 2.0, 3.0, 4.0]

```

Then

```
Printf.format(io, f, a)

```

give ma an error.

Is there a way, exactly the same as this example, just that I could print array a instead of manually inputting 1,2,3,4 here?

This could be very useful because in the code it was very likely that we need to print some array with certain 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:** [August 24, 2021, 5:51am UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/16 "2021-08-24T05:51:00Z")

</div>

> [@CRquantum](#):
>
> same example in the quote, if I put 1, 2, 3, 4 as an array

In this case, have you tried the [broadcasting solution above](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/8) but without `Ref` and replacing by the longer formatting sequence?

---

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 24, 2021, 6:01am UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/17 "2021-08-24T06:01:29Z")

</div>

Thank you very much!  
Yes I tried,

```
a = [1.0, 2.0, 3.0, 4.0]
f = Ref(Printf.Format("%5.2f"));
str = Printf.format.(f,a)

```

This gives me a 4-element Vector{String} which should be correct. However, when I write to a file by,

```
io = open("fileout.txt", "a") 
write(io, str)
close(io)

```

It gives an error

```
`write` is not supported on non-isbits arrays

```

It seems write can only operator on one string? I am sure write can handle an array but I am not sure how to do it.

---

<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:** [August 24, 2021, 6:38am UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/18 "2021-08-24T06:38:51Z")

</div>

@CRquantum, you can add `join`:  
(using also [join trick](https://discourse.julialang.org/t/how-to-declare-a-string-array-for-the-purpose-of-concatenation/66868/8) from @gustaphe):

```julia
a = [1.0, 2.0, 3.0, 4.0]
f = Ref(Printf.Format("%5.2f"));
open("fileout0.txt", "a") do io
    join(io, Printf.format.(f,a))
end

```

---

<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:** [August 24, 2021, 12:36pm UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/19 "2021-08-24T12:36:29Z")

</div>

Now I want a `join(io::IO, f::Format , x, delim)` method…

---

<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:** [August 24, 2021, 12:53pm UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/20 "2021-08-24T12:53:25Z")

</div>

`join` takes iterables, so something like

```julia
let f = Printf.Format("%5.2f")
    join(io, (Printf.format(f, a) for a in a))
end

```

should work fine.

[Next page](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695.md?page=2)
