def _table_alignment(alignment: str, n_columns: int) ->str:
    """Return a valid latex tabular alignment string.

    See :func:`write` vor valid examples.

    Args:
        alignment: Simplified string converted to the full table alignment.
        n_columns: Number of columns for which the alignment is created.
    """
    if not alignment:
        return 'c' * n_columns
    align_chars = 'lcr|'
    if set(alignment) - set(align_chars):
        raise ValueError(
            f"Invalid alignment '{alignment}'. Must only contain: {align_chars}."
            )
    alignment_chars = alignment.replace('|', '')
    n_alignment_chars = len(alignment_chars)
    if n_alignment_chars == n_columns:
        return alignment
    if n_alignment_chars == 1:
        raw_alignment = alignment_chars * n_columns
        if len(alignment) == 1:
            return raw_alignment
        if len(alignment) == 2:
            return '|'.join(raw_alignment)
        if len(alignment) == 3:
            return '|' + '|'.join(raw_alignment) + '|'
        raise ValueError(
            f"Too many | separators passed to alignment '{alignment}'. Must pass exactly 1 or 2."
            )
    raise ValueError(
        f'Number of alignment characters ({n_alignment_chars}) must match number of columns {{n_columns}}.'
        )
