ASCII Art Table Generator

  • Share this:

Code introduction


Converts the input text into an ASCII art table, where the text is split into multiple lines, each containing one character of the text.


Technology Stack : str

Code Type : Function

Code Difficulty : Intermediate


                
                    
def ascii_table(text, max_length=80):
    """
    将文本转换为ASCII艺术表。
    """
    # 获取文本的长度
    text_length = len(text)
    # 创建表头
    header = f"{'|':^5}" + '|'.join([f"{' ':^5}" * (max_length // text_length) for _ in range(text_length)]) + '|'
    # 创建分隔线
    separator = '-' * (len(header) - 1)
    # 将文本转换为ASCII艺术表
    ascii_table = header + '\n' + separator + '\n' + text + '\n' + separator
    return ascii_table                
              
Tags: