Print full ascii art

Question:

I am trying to print ascii art like this:

print(("""

                                       ._ o o
                                       _`-)|_
                                    ,""        
                                  ,"  ## |   ಠ ಠ. 
                                ," ##   ,-__    `.
                              ,"       /     `--._;)
                            ,"     ## /
                          ,"   ##    /


                    """).encode('utf-8'))

And the output does not look right at all.

What is the proper method of printing ascii art?

Asked By: user3476998

||

Answers:

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.

Answered By: user2357112

I get “…codec can’t encode character ‘u0ca0’ in position…”

If print(giraffe) fails due to an incorrect character encoding then try to set PYTHONIOENCODING environment variable correctly e.g., in bash:

$ PYTHONIOENCODING=utf-8 python3 -c 'from text_art import giraffe as s; print(s)'

Do not use print(giraffe.encode('utf-8')):

  • print() function expects a text, not bytes (unrelated: to print bytes, you could use sys.stdout.buffer.write(some_bytes))
  • how bytes are interpreted as a text is the property of your terminal, you shouldn’t hardcode its settings in your code. PYTHONIOENCODING allows you to change the encoding if necessary
Answered By: jfs
print(r"""

                                   ._ o o
                                   _`-)|_
                                ,""        
                              ,"  ## |   ಠ ಠ. 
                            ," ##   ,-__    `.
                          ,"       /     `--._;)
                        ,"     ## /
                      ,"   ##    /


                """)

The r allows you to print raw text better especially when there is a lot of inverted commas in the picture that you are trying to print.

No need to add ''.encode('utf-8')

I print ASCCII art all the time using Python 3.6.7 on Ubuntu machines

__header__ = '''Content-Type: application/xml

33[92m        .---------------------------------.
33[92m        |  .---------------------------.  |
33[92m        |[]|33[94m         __   __   *       33[92m|[]|
33[92m        |  |33[94m        /  | /  | /        33[92m|  |
33[92m        |  |33[94m       (___|(___|(         33[92m|  |
33[92m        |  |33[94m       |   )|    |         33[92m|  |
33[92m        |  |33[94m       |  / |    |         33[92m|  |
33[92m        |  |33[94m /              |         |33[92m|  |
33[92m        |  |33[94m(  ___  ___  ___| ___  ___|33[92m|  |
33[92m        |  |33[94m| |   )|   )|   )|___)|   )33[92m|  |
33[92m        |  |33[94m| |__/ |__/||__/ |__  |__/ 33[92m|  |
..And more

THEN

print(__header__)

And the result

Ubuntu-Python3-print(ASCII)

Answered By: Nathan Smiechowski
print(r"""

                               ._ o o
                               _`-)|_
                            ,""        
                          ,"  ## |   ಠ ಠ. 
                        ," ##   ,-__    `.
                      ,"       /     `--._;)
                    ,"     ## /
                  ,"   ##    /


            """)
print(r"""

                               ._ o o
                               _`-)|_
                            ,""        
                          ,"  ## |   ಠ ಠ. 
                        ," ##   ,-__    `.
                      ,"       /     `--._;)
                    ,"     ## /
                  ,"   ##    /


            """)
Answered By: Yash Shukla
print(r"""

                               ._ o o
                               _`-)|_
                            ,""        
                          ,"  ## |   ಠ ಠ. 
                        ," ##   ,-__    `.
                      ,"       /     `--._;)
                    ,"     ## /
                  ,"   ##    /


            """)

this will type or print this in raw text

Answered By: jacob

to solve this issue I just added the header ‘# coding=utf-8’ at the top level of the file and it works.
Code

Result of the script

Answered By: Luis Noe Raygoza C.