Display ASCII art in Python: strings and TXT files
For a small drawing, use a multiline string and print. For a reusable file, read TXT with an explicit encoding. Backslashes and leading spaces are part of the drawing: do not automatically remove them.
Example 1: a raw multiline string
art = r""" /\_/\ ( o.o ) > ^ <""" print(art)
The r prefix preserves the cat’s backslashes instead of interpreting escape sequences. The drawing starts on the opening line to avoid a blank first line. print adds a final line break. The Python string documentation describes raw and triple-quoted forms.
Example 2: read TXT without adding a line
from pathlib import Path
art = Path("dessin.txt").read_text(encoding="utf-8")
print(art, end="")Save the drawing as dessin.txt in the working directory. Path.read_text reads its contents; encoding="utf-8" makes the choice explicit. end="" prevents print from adding its own line break. If the file already ends with a newline, it remains; otherwise the command does not create one.
Measure an ASCII drawing
lines = art.splitlines() width = max((len(line) for line in lines), default=0) print(width, len(lines))
For the first example’s cat, the result is 7 3: at most seven columns and three lines. This works for printable ASCII without tabs. For Unicode, len counts code points, not necessarily displayed columns. A tab needs expansion using known tab stops before measurement.
Pitfalls to avoid
strip() also removes spaces at the start of the drawing and can shift the ears. A raw string cannot end with an odd number of backslashes immediately before its delimiter. If the drawing contains your chosen triple quotes, prefer a TXT file. Finally, do not make the text an f-string unnecessarily: braces in the drawing could be interpreted.
Scope and next step
These examples use Python 3 and install no library. They display prepared text; they do not convert an image into ASCII. The terminal still needs an appropriate font and sufficient width. Read the terminal guide or create TXT with the image converter.