Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Python to print beautiful forms

Shulou Source: shulou.com Published: 2022-06-03 01:14:31 09月18日 Update

This article mainly explains "how to use Python to print beautiful forms". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Python to print beautiful forms.

The first one: use format

Let's take a look at some little demo first.

Align left

> "{: 10}" .format ("a")'a'>

In the middle

> > "{: ^ 10}" .format ("a")'a'>

When you don't specify

< 、>

, ^, the default is left alignment

> > "{: 10}" .format ("a")'a'>

With the above bedding, it is easy to write a neat 1-10 square and cubic table.

> for x in range (1,11): Print ('{: 2d} {: 4d} '.format (x, xonomx, x*x*x))... 1 1 12 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000

The idea of alignment is to automatically fill in the blanks for you in the insufficient position.

If you don't want to use spaces, you can specify the characters you want to fill in, for example, I'll complete it with 0 below.

> for x in range (1,11): Print ('{: 02d} {: 04d} '.format (x, x, x*x*x))... 01 001 0001 02 004 0008 03 009 0027 04 16 0064 0125 06 036 07 049 0343 08 064 0512 081 0729 10 0001 1000

Second: use ljust, rjust left alignment

> "a" .ljust (10)'a'> >

Align right

> "a" .rjust (10)'a'>

In the middle

> "a" .center (10)'a'>

Also write a neat square and cube table of 1-10

> for x in range (1,11): Print (''.join ([str (x) .ljust (2), str (x * x) .ljust (3), str (x * x * x) .ljust (4)])... 1 1 12 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000

What if you don't want to use spaces and use 0 instead? You can do this.

> for x in range (1,11): Print (''.join ([str (x) .rjust (2, "0"), str (xintux) .rjust (3, "0"), str (x*x*x) .rjust (4, "0")). 01 001 0001 02 004 0008 03 009 04 016 025 0125 06 036 036 049 049 064 0512 081 0729 10 1000

The above is today's sharing. Many beautiful form printing modules are based on the above built-in functions. If you master these two, you can realize them on your own in the future. I hope this article will be helpful to you.

Welfare at the end of the article

Brother Ming sorted out 21 quick check lists of Python codes, each of which was summarized by foreign masters, which is very practical.

There are also 2 high-definition PyCharm shortcut keys at a glance, a Windows, a Mac, on the desktop, when you need to open it, very convenient.

At this point, I believe you have a deeper understanding of "how to use Python to print beautiful forms". 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!

Tags: Beautiful table that is spaces utility content learning deeper at a glance two code interest function master character practicality practical shortcut keys ideas simple operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Redmi MariaDB OPPO Reno Docker