If you would like to do it in another way, you can use String formatting. First build the string using an inner for loop, set the required width and print.