What's a quick way of generating a string like this?

A_01.jpg
A_02.jpg
...
A_16.jpg

Basically how do I let Julia add a zero in front of it, when it is a single digit number? Do I have to write an if loop? Thanks

You can use string:

(v1.9) julia> string(1, pad = 2)
"01"

(v1.9) julia> string(11, pad = 2)
"11"
3 Likes

You could use Printf.@sprintf.

julia> using Printf

julia> filename(n) = @sprintf "A_%02d.jpg" n
filename (generic function with 1 method)

julia> println.(filename.(1:16));
A_01.jpg
A_02.jpg
A_03.jpg
A_04.jpg
A_05.jpg
A_06.jpg
A_07.jpg
A_08.jpg
A_09.jpg
A_10.jpg
A_11.jpg
A_12.jpg
A_13.jpg
A_14.jpg
A_15.jpg
A_16.jpg
3 Likes