# 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:** 8\
**Page:** 2

<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, 1:01pm UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/21 "2021-08-24T13:01:23Z")

</div>

Yes, but that generates strings. `format` can operate on an `IO`, so it wouldn’t be too hard to define such a function that doesn’t go via strings at all.

---

<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, 1:09pm UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/22 "2021-08-24T13:09:22Z")

</div>

You are right. For avoiding even the intermediate strings, something like

```julia
function join2(io, f, itr, delim, last = delim)
    for (i, a) in enumerate(a)
        i == 0 || print(io, delim)
        f(io, a)
    end
    print(io, last)
end

```

which takes a two-argument function but encapsulates the logic of `join` would be nice.

---

<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, 1:56pm UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/23 "2021-08-24T13:56:59Z")

</div>

Swap the first two arguments and you can do

```julia
delimiter="\t"
numbers=randn(20)
fmt=Format("%.2f")
open("file.tsv", "w") do file
    join(file, numbers, delimiter) do io, x
        format(io, fmt, x)
    end
end

```

Not sure why you’d want to, but it looks pretty cool.

---

<div class="post-metadata">

**Author:** ![bensetterholm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bensetterholm/32/12792_2.png) [@bensetterholm](https://discourse.julialang.org/u/bensetterholm)\
**Post date:** [August 25, 2021, 3:33pm UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/24 "2021-08-25T15:33:01Z")

</div>

You can use the [splat](https://docs.julialang.org/en/v1/manual/faq/#The-two-uses-of-the-...-operator:-slurping-and-splatting) operator.

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

```

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [January 22, 2022, 6:36pm UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/25 "2022-01-22T18:36:49Z")

</div>

I’m confused about this one. When I tried it I got

```julia
julia> x=rand(3);

julia> fmt=Printf.Format("%.2f");

julia> Printf.format(fmt,x...)
ERROR: ArgumentError: mismatch between # of format specifiers and provided args: 1 != 3

```

What have I missed?

---

<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:** [January 22, 2022, 6:52pm UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/26 "2022-01-22T18:52:00Z")

</div>

Your format string only has one specifier, so it will only work on one argument. Depending on what output you’re expecting, there’s options like

```julia
julia> fmt=Printf.Format("%.2f"^3); # equivalent to "%.2f%.2f%.2f"

julia> Printf.format(fmt, x...)
"0.030.050.21"

```

or

```julia
julia> fmt=Printf.Format("%.2f");

julia> Printf.format.(Ref(fmt),x)
3-element Vector{String}:
"0.03"
"0.05"
"0.21"

```

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 16, 2023, 8:56am UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/27 "2023-02-16T08:56:36Z")

</div>

> [@lmiq](#):
>
> `println([@sprintf(" %5.2f",x) for x in x]...)`

Especially the three dots at the end (`splat operator`) help a lot 🙂

---

<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:** [March 16, 2023, 12:14pm UTC](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/28 "2023-03-16T12:14:52Z")

</div>

For the record, probably the best solution here is just write a double loop: [Writing array to file with format - #2 by stevengj](https://discourse.julialang.org/t/writing-array-to-file-with-format/96175/2)

```julia
julia> using Printf

julia> A = rand(3,3);

julia> # with io == stdout
       for i in axes(A,1)
           for j in axes(A,2)
               @printf(" %5.2f", A[i,j])
           end
           println() # new line
       end
  0.21 0.48 0.26
  0.12 0.92 0.17
  0.03 0.91 0.41

```

or to a file:

```julia
julia> open("test.txt","w") do io
           for i in axes(A,1)
               for j in axes(A,2)
                   @printf(io, " %5.2f", A[i,j])
               end
               println(io) # new line
           end
       end

shell> more test.txt
  0.21 0.48 0.26
  0.12 0.92 0.17
  0.03 0.91 0.41

```

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