import math


def encode(s):
    s = list(filter(str.isalnum, s.lower()))
    size = math.ceil(math.sqrt(len(s)))
    s += "." * (size**2 - len(s))
    parts = [s[i*size:(i+1)*size] for i in range(size)]
    return " ".join(map("".join, zip(*parts))).replace(".", "")

import math


def encode(s):
    s = "".join(filter(str.isalnum, s.lower()))
    size = math.ceil(math.sqrt(len(s)))
    return " ".join(s[i::size] for i in range(size))

Use string slices with a stride
Use string slices with a stride
