# How to align output in columns?

**URL:** <https://discourse.julialang.org/t/how-to-align-output-in-columns/3938>\
**Category:** New to Julia\
**Tags:** question, prettytables\
**Created:** [May 26, 2017, 9:58am UTC](https://discourse.julialang.org/t/how-to-align-output-in-columns/3938 "2017-05-26T09:58:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [May 26, 2017, 9:58am UTC](https://discourse.julialang.org/t/how-to-align-output-in-columns/3938/1 "2017-05-26T09:58:33Z")

</div>

I have the following script:

```julia
println("n \t n! \t\t Approximation \t\t Ratio")

for n in 1:10
    fact = factorial(big(n))
    stir = n^n * exp(-n) * sqrt(2 * π * n )
    ratio = Float32(fact/stir)
    println("$n \t $fact \t $stir \t $ratio")
end

```

This results in the following output:

```julia

n n! Approximation Ratio
1 1 0.9221370088957891 1.0844376
2 2 1.9190043514889832 1.0422071
3 6 5.836209591345864 1.0280645
4 24 23.50617513289329 1.0210083
5 120 118.01916795759008 1.016784
6 720 710.0781846421849 1.0139729
7 5040 4980.395831612461 1.0119678
8 40320 39902.3954526567 1.0104656
9 362880 359536.87284194824 1.0092984
10 3628800 3.598695618741036e6 1.0083654

```

Which is the better approach for cases like this one when I want to print outputs in columns?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 26, 2017, 10:42am UTC](https://discourse.julialang.org/t/how-to-align-output-in-columns/3938/2 "2017-05-26T10:42:42Z")

</div>

Perhaps:

```julia
using DataFrame

df = DataFrame(n = Int[], n! = Int[], Approximation = Float64[], Ratio = Float64[])

for n in 1:10
    fact = factorial(big(n))
    stir = n^n * exp(-n) * sqrt(2 * π * n )
    ratio = Float32(fact/stir)
    push!(df, [n, fact, stir, ratio])
end

strwidths = [maximum(strwidth.(string.([df[:, i]; names(df)[i]]))) for i in 1:size(df, 2)]

io = IOBuffer()

# Print headers
for (i, header) in enumerate(names(df))
	print(io, rpad(header, strwidths[i]), "\t")
end
println(io)

for j in 1:size(df, 1)
	for i in 1:size(df, 2)
		print(io, rpad(df[j,i], strwidths[i]), "\t")
	end
	println(io)
end

print(String(take!(io)))

```

giving

```julia
n n! Approximation Ratio             	
1 1 0.9221370088957891 1.084437608718872 	
2 2 1.9190043514889832 1.04220712184906  	
3 6 5.836209591345864 1.028064489364624 	
4 24 23.50617513289329 1.0210082530975342	
5 120 118.01916795759008 1.0167839527130127	
6 720 710.0781846421849 1.0139728784561157	
7 5040 4980.395831612461 1.0119677782058716	
8 40320 39902.3954526567 1.0104656219482422	
9 362880 359536.87284194824 1.0092984437942505	
10	3628800	3.598695618741036e6	1.0083653926849365	

```

The code is not very well polished but might give you a start.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [May 26, 2017, 11:58am UTC](https://discourse.julialang.org/t/how-to-align-output-in-columns/3938/3 "2017-05-26T11:58:20Z")

</div>

Thank you… I am sorry, I didn’t want you to write the whole code for me… I was just thinking there was something more concise… thank you for the the code and the many ideas on it…

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 27, 2017, 5:54pm UTC](https://discourse.julialang.org/t/how-to-align-output-in-columns/3938/4 "2017-05-27T17:54:46Z")

</div>

You might find it easier to use something like [GitHub - JuliaString/StringLiterals.jl: Implement improved string literals with Swift-style syntax for interpolation, hex, & unicode characters, plus C & Python style formatting and Unicode, HTML, LaTeX, and Emoji entities](https://github.com/JuliaString/StringLiterals.jl),  
and I think it gives cleaner, easier to read output than the other solution.

It is not registered, so you need to load it and it’s dependencies the first time as follows:

```julia
const loc = "https://github.com/JuliaString/"
for pkg in ("StrTables",
            "HTML_Entities", "LaTeX_Entities", "Unicode_Entities", "Emoji_Entities",
            "Format", "StringLiterals")
    Pkg.clone(string(loc, pkg, ".jl"))
end

```

Then you can write formatted output very succintly:

```julia
using StringLiterals

function out()
    pr"""n \%8s("n!") \%16s("Approximation") \%10s("Ratio")\n"""
    for n in 1:10
        fact = factorial(big(n))
        stir = n^n * exp(-n) * sqrt(2 * π * n )
        ratio = Float32(fact/stir)
        pr"\%-3d(n) \%8d(fact) \%16.8f(stir) \%10.8f(ratio)\n"
    end
end

```

The output is:

```julia
n n! Approximation Ratio
1 1 0.92213701 1.08443761
2 2 1.91900435 1.04220712
3 6 5.83620959 1.02806449
4 24 23.50617513 1.02100825
5 120 118.01916796 1.01678395
6 720 710.07818464 1.01397288
7 5040 4980.39583161 1.01196778
8 40320 39902.39545266 1.01046562
9 362880 359536.87284195 1.00929844
10 3628800 3598695.61874104 1.00836539

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 27, 2017, 10:05pm UTC](https://discourse.julialang.org/t/how-to-align-output-in-columns/3938/5 "2017-05-27T22:05:32Z")

</div>

Yeah, using seven packages and hard coding the alignments will indeed make the code shorter.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [May 27, 2017, 11:55pm UTC](https://discourse.julialang.org/t/how-to-align-output-in-columns/3938/6 "2017-05-27T23:55:38Z")

</div>

Another option might be to use `@printf`

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 28, 2017, 12:11am UTC](https://discourse.julialang.org/t/how-to-align-output-in-columns/3938/7 "2017-05-28T00:11:16Z")

</div>

Many packages depend on others - the only issue is that the packages are not yet registered.  
No need to be snide.

---

<div class="post-metadata">

**Author:** ![JoshuaWhittemore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshuawhittemore/32/12189_2.png) [@JoshuaWhittemore](https://discourse.julialang.org/u/JoshuaWhittemore)\
**Post date:** [January 7, 2020, 6:55pm UTC](https://discourse.julialang.org/t/how-to-align-output-in-columns/3938/8 "2020-01-07T18:55:49Z")

</div>

Using `@printf` as suggested:

```julia
julia> using Printf

julia> function justified()
           @printf("%10s %10s %-20s %s \n", "n", "n!", "Approximation", "Ratio")

           for n in 1:10
               fact = factorial(big(n))
               stir = n^n * exp(-n) * sqrt(2 * π * n )
               ratio = Float32(fact/stir)
               @printf("%10d %10d %-20s %f \n", n, fact, string(stir), ratio)
           end
       end
justified (generic function with 1 method)

julia> justified()
         n n! Approximation Ratio
         1 1 0.9221370088957891 1.084438
         2 2 1.9190043514889832 1.042207
         3 6 5.836209591345864 1.028064
         4 24 23.50617513289329 1.021008
         5 120 118.01916795759008 1.016784
         6 720 710.0781846421849 1.013973
         7 5040 4980.395831612461 1.011968
         8 40320 39902.3954526567 1.010466
         9 362880 359536.87284194824 1.009298
        10 3628800 3.598695618741036e6 1.008365
        

```

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [January 7, 2020, 7:47pm UTC](https://discourse.julialang.org/t/how-to-align-output-in-columns/3938/9 "2020-01-07T19:47:39Z")

</div>

There is also the wonderful PrettyTables package…

```julia
julia> using PrettyTables
julia> pretty_table(df;formatter=ft_printf(["%10d","%10d","%5.6f","%5.6f"],[1,2,3,4]),alignment=[:r,:r,:l,:l])
┌────────────┬────────────┬────────────────┬──────────┐
│ n │ n! │ Approximation │ Ratio │
│ Int64 │ Int64 │ Float64 │ Float64 │
├────────────┼────────────┼────────────────┼──────────┤
│ 1 │ 1 │ 0.922137 │ 1.084438 │
│ 2 │ 2 │ 1.919004 │ 1.042207 │
│ 3 │ 6 │ 5.836210 │ 1.028064 │
│ 4 │ 24 │ 23.506175 │ 1.021008 │
│ 5 │ 120 │ 118.019168 │ 1.016784 │
│ 6 │ 720 │ 710.078185 │ 1.013973 │
│ 7 │ 5040 │ 4980.395832 │ 1.011968 │
│ 8 │ 40320 │ 39902.395453 │ 1.010466 │
│ 9 │ 362880 │ 359536.872842 │ 1.009298 │
│ 10 │ 3628800 │ 3598695.618741 │ 1.008365 │
└────────────┴────────────┴────────────────┴──────────┘

```
