# How to create tab space using @sprintf?

**URL:** <https://discourse.julialang.org/t/how-to-create-tab-space-using-sprintf/46421>\
**Category:** General Usage\
**Created:** [September 10, 2020, 11:06pm UTC](https://discourse.julialang.org/t/how-to-create-tab-space-using-sprintf/46421 "2020-09-10T23:06:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [September 10, 2020, 11:06pm UTC](https://discourse.julialang.org/t/how-to-create-tab-space-using-sprintf/46421/1 "2020-09-10T23:06:58Z")

</div>

For some reasons, I would like to stick to using sprintf and I need to write into a file when each line has a tab space in the beginning. Here is what I wrote but it doesn’t work:

```julia
ff = open("test.txt", "w")

ss = Array{String, 0}

ss = @sprintf("\t o%-6d I : %9.6f, %9.6f", val[jj], 5.000000, -5.000000)

print(ff, ss)

```

instead of creating the space, it just writes “\t” into the file.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 10, 2020, 11:14pm UTC](https://discourse.julialang.org/t/how-to-create-tab-space-using-sprintf/46421/2 "2020-09-10T23:14:21Z")

</div>

Why writing to an array of strings? You can write directly to the file:

```julia
julia> open("test.txt", "w") do io
           for i in 1:5
               @printf(io, "\t o%-6d I : %9.6f, %9.6f\n", i, rand(), -rand())
           end
       end

shell> cat test.txt
         o1 I : 0.499033, -0.518227
         o2 I : 0.452359, -0.057112
         o3 I : 0.137529, -0.954224
         o4 I : 0.792647, -0.996261
         o5 I : 0.859669, -0.768580

```

---

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [September 10, 2020, 11:29pm UTC](https://discourse.julialang.org/t/how-to-create-tab-space-using-sprintf/46421/3 "2020-09-10T23:29:05Z")

</div>

This actually works. Thank you.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 10, 2020, 11:34pm UTC](https://discourse.julialang.org/t/how-to-create-tab-space-using-sprintf/46421/4 "2020-09-10T23:34:44Z")

</div>

BTW, I can’t reproduce the problem you were saying:

```julia
julia> ff = open("test.txt", "w")
IOStream(<file test.txt>)

julia> ss = @sprintf("\t o%-6d I : %9.6f, %9.6f", 10, 5.000000, -5.000000)
"\t o10 I : 5.000000, -5.000000"

julia> print(ff, ss)

julia> close(ff)

shell> cat test.txt
         o10 I : 5.000000, -5.000000

```

---

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [September 10, 2020, 11:44pm UTC](https://discourse.julialang.org/t/how-to-create-tab-space-using-sprintf/46421/5 "2020-09-10T23:44:09Z")

</div>

This is what I get:

```julia
@sprintf("\t o%-6d %s :%9.6f %9.6f", val[jj], SS, 5.000000, -5.000000)
"\t o197239 I : 5.000000 -5.000000"

```

My Julia version is 1.2.0

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 10, 2020, 11:50pm UTC](https://discourse.julialang.org/t/how-to-create-tab-space-using-sprintf/46421/6 "2020-09-10T23:50:45Z")

</div>

Well, that’s normal: you write out a string which contains a literal `\t`, but when you print it the `\t` is interpreted as a tab:

```julia
julia> ss = "\t o197239 I : 5.000000 -5.000000"
"\t o197239 I : 5.000000 -5.000000"

julia> println(ss)
         o197239 I : 5.000000 -5.000000

```

---

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [September 11, 2020, 12:13am UTC](https://discourse.julialang.org/t/how-to-create-tab-space-using-sprintf/46421/7 "2020-09-11T00:13:30Z")

</div>

Got it. Thank you.
