You can download this code by clicking the button below.
This code is now available for download.
This function takes a string as input and generates its ASCII art representation. By scaling the width of letters and characters, it converts the text into an ASCII art.
Technology Stack : Built-in libraries
Code Type : Function
Code Difficulty : Intermediate
def ascii_table(text):
"""
输入文本,生成ASCII字符画。
"""
import string
def get_char_width(char):
return len(char) if char in string.ascii_letters else 2
def scale_text(text, scale):
return ''.join([char * scale for char in text])
def create_row(text, scale):
row = ''
for char in text:
if char in string.ascii_letters:
row += scale_text(char, scale)
else:
row += ' ' * (get_char_width(char) * scale)
return row
scale = 2
max_width = max(len(line) for line in text.split('\n'))
ascii_art = '\n'.join(create_row(line, scale) for line in text.split('\n'))
return ascii_art