How to easily print ascii-art text?
pyfiglet - pure Python implementation of http://www.figlet.org
pip install pyfiglet
termcolor - helper functions for ANSI color formatting
pip install termcolor
colorama - multiplatform support (Windows)
pip install colorama
import sys
from colorama import init
init(strip=not sys.stdout.isatty()) # strip colors if stdout is redirected
from termcolor import cprint
from pyfiglet import figlet_format
cprint(figlet_format('missile!', font='starwars'),
'yellow', 'on_red', attrs=['bold'])
Example
$ python print-warning.py
$ python print-warning.py | cat
.___ ___. __ _______. _______. __ __ _______ __
| \/ | | | / | / || | | | | ____|| |
| \ / | | | | (----` | (----`| | | | | |__ | |
| |\/| | | | \ \ \ \ | | | | | __| | |
| | | | | | .----) | .----) | | | | `----.| |____ |__|
|__| |__| |__| |_______/ |_______/ |__| |_______||_______|(__)
How do I properly print ASCII art?
The main problem in your code is that the \
at the end of the lines is interpreted as a line continuation, so the next line just gets concatenated to the previous one.
You can avoid this by adding any character at the end of the line, after the backslash, like a space for example (as the line continuation is triggered only if the backslash is the last character in the line).
Another solution is to use a raw string, starting with r""" ....
. As stated in the documentation:
Specifically, a raw literal cannot end in a single backslash (since
the backslash would escape the following quote character). Note also
that a single backslash followed by a newline is interpreted as those
two characters as part of the literal, not as a line continuation. (emphasis mine)
This will also prevent any potential escape sequence starting with a backslash to be interpreted as such.
Print full ascii art
encode
takes a string and encodes it into bytes. That's not what you want here; you want to just print the string directly:
print("""\
._ o o
\_`-)|_
,"" \
," ## | ಠ ಠ.
," ## ,-\__ `.
," / `--._;)
," ## /
," ## /
""")
If this doesn't work, your terminal is most likely not configured to display Unicode. Unfortunately, I am not particularly knowledgeable about terminal configuration; Why doesn't my terminal output unicode characters properly? may be relevant, but my ability to help is mostly limited to the Python side of things.
How to print ASCII art?
The example you posted appears to print fine in both python 2.7 and python 3. It's unclear where your issue is occurring since this works fine, but for other ASCII text perhaps you are getting the error.
In order to print ASCII text that contains quotes like ' or " you need to add the triple quotes at the beginning and end of the print function to fix this.
Maybe your issue is that you don't want to have the new lines at the top and bottom of the ascii art. You need to have a space at the end to let python know it is the end of the string. For example:
print(''''art' ''')
Notice there are 3 quotes at the start and end, with a space between the last quote in the string you are printing. This removes the newline from the string.
This won't work since it does not have the space:
print(''''art'''')
To get rid of the top newline, you'll basically need to press backspace on the first line of the top of your art but preserve the spaces. It won't line up in the code, but when it prints it will line up correctly.
This code removes the top and bottom newlines. Maybe you were forgetting to add the space at the end between the quote in the ascii art and the triple quote.
mystring = ''' .$$$$$:$$$:$$$$$$$ _..._ .$$$SSSSSS$$$$$$$$$.
.$$$$$:$$$$:$$$$$$$ ~.sggg. " .~(g )$$$$$$$$$$.
$$$$$:$$$$$:$$$$$$$ .sS$$$$$$$$s. : '"--"' `$$$$$$$$$$.
`$$$:$$$$$$:$$$$$$$.$$" .. g"-. `. `.-.._ `$$$$$$$$$$
$$$:$$$$$$:$$$$$$$`$' ' `._.' : `--- $$$$$$$$$.
$$$:.$$$$$:$$$$$$$ `---' _.' $$$$$$$$$$$.
$$$$$:$$$$:$$$$$$s ----" . $$$$$$$$$$$$.
$$$$$`.$$$:$$$$$$$. `-._ .$$$$$$$$$$$$$$Sss.
$$$$$$`;$$:$$$$$$$$. _.: .' ; $$$$$$$$$$$$$$$$$$$.
.s$$$$$$'$$`.$$$$$$$$. .' `. ' _ .`. $$$$$$$$$$$$$$$$$$$$Ss.
.s$$$$$$$$$$$$:$$$$$$$$$ : _ ~~-...'.'.' : $$$$$$$$$$$$$$$$$$$$$$$
.s$$$$$$$$$$$$$$`.$$$$$$$$s : .~-,-.-.~:'.' : $$$$$$$$$$$$$$$$$$$$$$
.s$$$$$$$$$$$$$$$$$`$$$$$$$$$$. ` ~-.`"""'.' `.$$$$$$$$$$$$$$$$$$$' '''
print(mystring)
Generate Ascii art text in C
Yes, this is a well known problem. The last time I solved this is to use an array for each line and rendering each letter separately.
Firstly, I would represent each letter in an array. For example your A would be something like this:
char* letter[8];
letter[0] = " _/_/ ";
letter[1] = " _/ _/";
etc.
(Actually a 2D array would be used where each letter is at a different index.)
The actual render would be in an array as well, something along the lines of:
char* render[8];
and then use concatenation to build each line. A simply nested for loop should do the trick:
for each line, i to render (i.e the height of the letters)
for each letter, j
concatenate line i of letter j to the to char* i in the array
Finally loop though the array and print each line. Actually, you can skip the render array and simply print each line without a carriage return. Something like the following comes to mind:
for each line, i to render : // (i.e the height of the letters)
for each letter, j {
output line i of letter j
}
output a carriage return
}
(My C is a bit rusty, from there the "pseudo" code. However, I think my logic is sound.)
Can't print ASCII art
Given your code print_ascii_art.py
turtle = (r'''\ ___-------___
_-~~ ~~-_
_-~ /~-_
/^\__/^\ /~ \ / \
/| O|| O| / \_______________/ \
| |___||__| / / \ \
| \ / / \ \
| (_______) /______/ \_________ \
| / / \ / \
\ \^\\ \ / \ /
\ || \______________/ _-_ //\__//
\ ||------_-~~-_ ------------- \ --/~ ~\ || __/)
~-----||====/~ |==================| |/~~~~~
(_(__/ ./ / \_\ \.
(_(___/ \_____)_)''')
print(turtle)
Open a command prompt and navigate in the folder where the file of interest is.
Once you're in the right directory type:
python print_ascii_art.py
This should fix your problem.
NOTE: If you're willing to run your script by just double clicking on it, you should add the line input()
at the end of your code. In this way the ascii art will be printed and the program will wait for any keyboard input before closing.
Python: printing ascii art from text file, backslashes are being doubled
I think it might be because of the file mode being rb
instead of r
but it's really a guess until you post the actual ascii_art.txt
:
with open('ascii_art.txt', 'r') as f:
for line in f:
print(line.rstrip())
Related Topics
Missing 1 Required Positional Argument - Issue
Python - Using Regex to Find Multiple Matches and Print Them Out
Python 2D List Performance, Without Numpy
How to Serialize Sqlalchemy Result to Json
Python Pandas Read_Excel() Module Not Found
Convert the String 2.90K to 2900 or 5.2M to 5200000 in Pandas Dataframe
Valueerror: X and Y Must Be the Same Size
Deleting Dataframe Row in Pandas If a Combination of Column Values Equals a Tuple in a List
Split String in a Spark Dataframe Column by Regular Expressions Capturing Groups
How to Further Filter a Result of Resultset
Most Efficient Way to Construct Similarity Matrix
Why Does Tkinter Image Not Show Up If Created in a Function
Split List into Lists Based on a Character Occurring Inside of an Element
How to Insert a Checkbox in a Django Form
Format/Suppress Scientific Notation from Pandas Aggregation Results
Unable to Install Psycopg2 (Pip Install Psycopg2)
How to Enable Autocomplete (Intellisense) for Python Package Modules