# @sprintf() does not work inside a for loop

**URL:** <https://discourse.julialang.org/t/sprintf-does-not-work-inside-a-for-loop/78126>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 19, 2022, 11:50am UTC](https://discourse.julialang.org/t/sprintf-does-not-work-inside-a-for-loop/78126 "2022-03-19T11:50:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LispyArnuld](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lispyarnuld/32/34670_2.png) [@LispyArnuld](https://discourse.julialang.org/u/LispyArnuld)\
**Post date:** [March 19, 2022, 11:50am UTC](https://discourse.julialang.org/t/sprintf-does-not-work-inside-a-for-loop/78126/1 "2022-03-19T11:50:26Z")

</div>

@sprintf() does not work inside for loop. I wonder why.

```julia
julia> using Printf

julia> for k in 1:10
           @sprintf("%.4f, ", sin(k))
       end

julia> @sprintf("%.4f, ", sin(2))
"0.9093, "

julia> for k in 1:10
           print(sin(k))
       end
0.84147098480789650.90929742682568170.1411200080598672-0.7568024953079282-0.9589242746631385-0.279415498198925860.65698659871878910.98935824662338180.4121184852417566-0.5440211108893698
julia> VERSION
v"1.7.2"
 

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [March 19, 2022, 11:55am UTC](https://discourse.julialang.org/t/sprintf-does-not-work-inside-a-for-loop/78126/2 "2022-03-19T11:55:41Z")

</div>

```julia
help?> @sprintf
  @sprintf("%Fmt", args...)

  Return @printf formatted output as string.

```

It returns a string, so:

```julia
julia> for k in 1:10
                println( @sprintf("%.4f, ", sin(k)) )
              end
0.8415,
0.9093,
0.1411,
-0.7568,
-0.9589,
-0.2794,
0.6570,
0.9894,
0.4121,
-0.5440,

```

---

<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:** [March 19, 2022, 11:59am UTC](https://discourse.julialang.org/t/sprintf-does-not-work-inside-a-for-loop/78126/3 "2022-03-19T11:59:23Z")

</div>

> [@oheil](#):
>
> `println( @sprintf("%.4f, ", sin(k)) )`

Better to jut use `@printf`.

```julia-auto
julia> for k in 1:10
           @printf("%.4f, ", sin(k))
       end
0.8415, 0.9093, 0.1411, -0.7568, -0.9589, -0.2794, 0.6570, 0.9894, 0.4121, -0.5440, 

```

---

<div class="post-metadata">

**Author:** ![LispyArnuld](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lispyarnuld/32/34670_2.png) [@LispyArnuld](https://discourse.julialang.org/u/LispyArnuld)\
**Post date:** [March 19, 2022, 12:00pm UTC](https://discourse.julialang.org/t/sprintf-does-not-work-inside-a-for-loop/78126/4 "2022-03-19T12:00:09Z")

</div>

Oh… so string is not printed automatically. I get it now

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [March 19, 2022, 12:27pm UTC](https://discourse.julialang.org/t/sprintf-does-not-work-inside-a-for-loop/78126/5 "2022-03-19T12:27:53Z")

</div>

What you see printed in the REPL is the return value of your expression.  
So if you build a structure that contains all the strings you will see it as well.  
Printing on the other hand really produces output even if you are not running the program in the REPL.

Example in the REPL:

```julia
julia> using Printf

julia> map(1:3) do k
           println("hello!")
           @sprintf("%d",k)
       end
hello!
hello!
hello!
3-element Vector{String}:
 "1"
 "2"
 "3"

```

Same code without the REPL:

```julia
$ julia -e 'using Printf;map(1:3) do k;println("hello!");@sprintf("%d",k);end'
hello!
hello!
hello!

```
