ASCII Table Centering Function

  • Share this:

Code introduction


This function takes a string and a width parameter to generate a centered ASCII table.


Technology Stack : String handling, list comprehension, max function, ljust function, center function

Code Type : Text processing

Code Difficulty : Intermediate


                
                    
def ascii_table(text, width=80):
    """
    输出给定文本的ASCII表格。
    """
    def get_ascii_table(text, width):
        lines = text.split('\n')
        max_length = max(len(line) for line in lines)
        ascii_table = ''
        for line in lines:
            padded_line = line.ljust(max_length)
            ascii_table += padded_line.center(width) + '\n'
        return ascii_table.strip()

    return get_ascii_table(text, width)