def padded_bin(k, n):
    """
    Converts a number to binary form, left-padded to a given length.

    This function might not work correctly with negative integers.

    Parameters
    ----------
    k : integer
        The number to be converted to binary.

    n : integer
        The target length of the binary string. The returned string
        will be at least of this length.

    Returns
    -------
    s : string
        The binary string, which is at least `n` characters long.
    """
    s = bin(k)
    return '0' * (n - len(bin(k)) + 2) + s[2:]