Encode the column cipher Given a string to encode, and a number of columns (the key), encode the string as follows:

I will be using the example Hello, world! and the key 3

First, write out the number of columns:

1 2 3


Next, write the text starting from the upper left, one character per column, and when you run out of columns, go to the next line.

1 2 3
H e l
l o ,
  w o
r l d
!


Now, to get your encoded text, read the text starting from the top left corner, but this time, first read one column, and then the next, and so on. If there is not a character in the slot, put a space.

Hl r!eowl l,od 


Note that here, there is a trailing space.

This is your encoded text.

Another test case is Programming Puzzles and Code Golf SE with key 5:

1 2 3 4 5
P r o g r
a m m i n
g   P u z
z l e s  
a n d   C
o d e   G
o l f   S
E


The encoded text is PagzaooErm lndl omPedef gius    rnz CGS.

More test cases

"abcdefghijklmnopqrstuvwxyz", 2 -&gt; "acegikmoqsuwybdfhjlnprtvxz"
"codegolf.stackexchange.com", 4 -&gt; "cg.ccgoooskhemdltea. efaxnc "
"Pen Pineapple Apple Pen!!!", 7 -&gt; "PeAeeapnnpp! pl!Ple!ie  n P "
"1,1,2,3,5,8,13,21,34,55,89", 10 -&gt; "18,,,5115,3,2,8,2931 ,, 53 ,4 "


Remember, this is code-golf, so the code with the fewest bytes wins.
MATL, 4 bytes

e!1e


Try it online!

This is about as straightforward as possible. e is a builtin that reshapes a matrix into n rows. So we read in input1 as a string, and shape into a matrix with input2 rows:

Hl r!
eowl 
l,od 


Then, we transpose it to get this:

Hel
lo,
 wo
rld
!  


After that, we just call 1e to reshape it into a single row, and display it as a string.

As you can see in this sample program, the "reshape" function conveniently adds as many zeroes as necessary for the matrix to be a perfect rectangle. When displaying as a string, MATL treats '0's as spaces, so this automatically fills in the necessary number of spaces for no extra work.