# Can I define string as "%Fmt" in Printf?

**URL:** <https://discourse.julialang.org/t/can-i-define-string-as-fmt-in-printf/66766>\
**Category:** General Usage\
**Tags:** io\
**Created:** [August 21, 2021, 7:17am UTC](https://discourse.julialang.org/t/can-i-define-string-as-fmt-in-printf/66766 "2021-08-21T07:17:04Z")\
**Posts on this page:** 10\
**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 21, 2021, 7:17am UTC](https://discourse.julialang.org/t/can-i-define-string-as-fmt-in-printf/66766/1 "2021-08-21T07:17:04Z")

</div>

Sorry for another stupid question.  
It is a little similar with [How to repeat the format in @printf? - #12 by CRquantum](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/12)  
However i want to be more creative by design my own fmts in Printf.

The idea is simply, I looked at [Printf · The Julia Language](https://docs.julialang.org/en/v1/stdlib/Printf/)  
There is a simply example there,

```
@printf("%f %F %f %F", Inf, Inf, NaN, NaN)

```

output is  
Inf Inf NaN NaN

Now, my idea is simply, it looks like “%f %F %f %F” is a string,  
so I think i can do

```
s = string("%f %F %f %F")
@printf(s, Inf, Inf, NaN, NaN)

```

It should give the same results.  
But it gives me error.  
However I think if it works it can be powerful because we can define anything using string concatenation like ^ \* etc.

Could anyone give some hints, is it possible to do things like above?

I am thinking I may do thinks like

```
define something
@printf( "$(something)", Inf, Inf, NaN, NaN)

```

Is it possible?

Thank you very much in advance.

---

<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, 7:38am UTC](https://discourse.julialang.org/t/can-i-define-string-as-fmt-in-printf/66766/2 "2021-08-21T07:38:37Z")

</div>

It seems to be the same problem with the same solution as in the other post:

```julia
s = Printf.Format("%f %F %f %F");
Printf.format(s, Inf, Inf, NaN, NaN)

```

---

<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, 7:45am UTC](https://discourse.julialang.org/t/can-i-define-string-as-fmt-in-printf/66766/3 "2021-08-21T07:45:32Z")

</div>

Thank you very much. Sorry I was a little sleepy and asked a stupid question again.  
I see, the great thing of Printf.Format is that it allows me to put string() inside, like,

```
s = Printf.Format(string("%f"^4,"%f"^4))

```

So can define anything.

May I ask, how to use or is it possible to use Printf.format to write the contents in a txt file, say AA.txt?

---

<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:02am UTC](https://discourse.julialang.org/t/can-i-define-string-as-fmt-in-printf/66766/4 "2021-08-21T08:02:10Z")

</div>

A very basic example below.  
Recommend you search discourse for more complete examples.

```julia
str = Printf.format(s, Inf, Inf, NaN, NaN)

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

```

or better:

```julia
io = open("fileout.txt", "a");
    Printf.format(io, s, Inf, Inf, NaN, NaN)
close(io);

```

---

<div class="post-metadata">

**Author:** ![trilobit](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@trilobit](https://discourse.julialang.org/u/trilobit)\
**Post date:** [August 21, 2021, 12:48pm UTC](https://discourse.julialang.org/t/can-i-define-string-as-fmt-in-printf/66766/5 "2021-08-21T12:48:24Z")

</div>

With @sprintf it is more compatible with “R”…and IMO simpler.

 ![img](https://global.discourse-cdn.com/julialang/original/3X/c/d/cdc8cf9bead1016d4c28feccce74813b2fb2e467.webp)

---

<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, 1:23pm UTC](https://discourse.julialang.org/t/can-i-define-string-as-fmt-in-printf/66766/6 "2021-08-21T13:23:43Z")

</div>

Your example does not seem to answer OP’s question. See the post linked by OP, the eval method and this [comment](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695/6).

---

<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:** [August 22, 2021, 1:47pm UTC](https://discourse.julialang.org/t/can-i-define-string-as-fmt-in-printf/66766/7 "2021-08-22T13:47:36Z")

</div>

Maybe the do-block form of `open` would be better to suggest?

```julia
open("fileout.txt", "a") do io
    Printf.format(io, s, Inf, Inf, NaN, NaN)
end

```

That way the file handle is still closed in case `format` throws an exception (using a `try`-`finally` block under the hood).

---

<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 22, 2021, 2:38pm UTC](https://discourse.julialang.org/t/can-i-define-string-as-fmt-in-printf/66766/8 "2021-08-22T14:38:37Z")

</div>

While format strings are a powerful DSL, I would stop short of including conditionals and control flow and use Julia instead. That is, if you need to construct a format string on the fly, just encapsulate the logic in a function that branches to the appropriate `@printf "%f" value` or `@printf "%F" value`.

---

<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:54am UTC](https://discourse.julialang.org/t/can-i-define-string-as-fmt-in-printf/66766/9 "2021-08-24T03:54:11Z")

</div>

> [@ericphanson](#):
>
> ```julia
> open("fileout.txt", "a") do io
> Printf.format(io, s, Inf, Inf, NaN, NaN)
> end
> 
> ```

Thank you very much indeed!

May I ask one more thing, if I set array A as

```
A = [Inf, Inf, NaN, NaN] 

```

can I just print this array in a way like

```
Printf.format(io, s, A) ?

```

---

<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:** [August 24, 2021, 10:37am UTC](https://discourse.julialang.org/t/can-i-define-string-as-fmt-in-printf/66766/10 "2021-08-24T10:37:18Z")

</div>

> [@CRquantum](#):
>
> `Printf.format(io, s, A)`

I think this should work:

```julia
Printf.format(io, s, A…)

```
