What is the method for Python to output a fancy string atlas with three lines of code
The main content of this article is to explain "what is the method for Python to output a fancy string atlas with 3 lines of code". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the method of Python outputting a fancy string atlas with 3 lines of code?"
The ancestor of string atlas: figlet
There is a fun command under Linux: figlet this command is to print out some information in a large font.
This command is interesting because it supports many kinds of character fancy output. Here are a few examples.
Why is Python so popular? it is because there are too many modules in this product, so figlet certainly can not escape the Wuzhishan of Python. Let's talk about how Python implements the function of figlet.
Pyhton implementation of figlet
It has been the same pattern for thousands of years. The python implementation of the yaml module is pyyaml, so everyone should have guessed that the python module of figlet is pyfiglet. Let's take a look at his official website: https://pypi.org/project/pyfiglet/.
Because it is not a fruitful module, there is no special explanation, in which I see this paragraph:
USAGE
You can use pyfiglet in one of two ways. First, it operates on the commandline as C figlet does and supports most of the same options. Run with-- help to see a full list of tweaks. Mostly you will only use-f to change the font. It defaults to standard.flf.
Tools/pyfiglet 'text to render'
Pyfiglet is also a library that can be used in python code:
From pyfiglet import Figlet f = Figlet (font='slant') print f.renderText ('text to render')
Three lines of code, can achieve the printing of character text, is not very simple? Try downloading the module. Module download: pip install pyfiglet
How to use pyfiglet
Let's first print according to the example to see the effect:
From pyfiglet import Figlet f = Figlet (font='slant') print (f.renderText ('Python')) output: _ / _ / _ / / / /
Sometimes, pretending to be B is so effortless. So, what other functions does it have? Let's look at its source code:
Class Figlet (object): "Main figlet class." Def _ _ init__ (self, font=DEFAULT_FONT, direction='auto', justify='auto',width=80):... Def main (): parser = OptionParser (version=__version__, usage='%prog [options] [text..]') Parser.add_option ('- favored,'--font', default=DEFAULT_FONT, help='font to render with (default:% default), metavar='FONT') parser.add_option ('- left-to-right','--direction', type='choice', choices= ('auto',' left-to-right', 'right-to-left') Default='auto', metavar='DIRECTION', help='set direction text will be formatted in'(default:% default)')
Figlet provides four fields: font, direction, justfity and width. I just said that there are many fancy fonts in this module. Let's see how many there are:
From pyfiglet import Figlet FigletFont print (FigletFont (). GetFonts ()) f = Figlet (font='5lineoblique') print (f.renderText ('Breeze Python')) output: / /) / / /)) / / _ _)) / / / _) / / / _) / /)) / / / _ / / / ((_ / / _) _ _ / /) / _ / / /) / / ((_ _ / / / ((_ _)
With so many fonts, if you take a test, you can play all day.
When you look at the source code, you should note that as long as you have similar modules such as OptionParser and argparse, they can basically be executed directly on the command line. If you don't believe it, look:
Other implementation and expansion
The above implementation has been extremely simple, so what is more convenient and fun? Recommend two websites:
Online character conversion: http://patorjk.com/software/taag
ASCII Art (Picture) Collection: https://www.bootschool.net/ascii-art
Bootschool can not only generate online atlas, but also collect a lot of interesting character paintings, which we can get through its ascii WordArt page tab, such as I choose characters:
At this point, I believe you have a deeper understanding of "what is the method of Python outputting a fancy string atlas with 3 lines of code?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!