Several methods of formatting output mainly introduce the use of the Format function

Source: Internet
Author: User

1 str self-function formatted output rjust () ljust () center () Zfill ()


The Zfill is zero-padded


Introduced:

Rjust (...)

S.rjust (width[, Fillchar]), string


Format the output to the string, do not handle the string itself, just return a formatted string, you can specify the string width, and specify the padding character, the default padding space!


Action: Aligning the output


Example:

in [+]: s1= "1" in [+]: s2= "1234" in [Page]: s1out[28]: ' 1 ' in []: s2out[29]: ' 1234 ' in [+]: For i in S1,S2:        ....: Print I ...: 11234# left-aligned output, less than 4, default padding space in [+]: For i in S1,s2: ....: Print I.rjust (4) ....: 11234


#填充其他字符

in [+]: S1.rjust (4, "*") out[35]: ' ***1 '


#补齐0

In [a]: S1.zfill (4) out[38]: ' 0001 '

Equals

In [MAX]: S1.rjust (4, ' 0 ') out[40]: ' 0001 '

2 float type Specify width output

In [approx]: 1.0/3out[36]: 0.33333333333333331In [PNS]: "%.3f"% (1.0/3) out[37]: ' 0.333 '


3 Format function formatted output, format is a bit like C language, substitution of variables

It is also a function of STR, which can be used more and more

It replaces% with {} and:.


By location

in [+]: print ' {0} is a {1} '. Format (' Cat ', ' animal ') cat is a animal


in [+]: print ' {1} is a {0} '. Format (' Cat ', ' animal ') animal is a cat


Explanation: The contents of {} will be replaced by the parameters in format (), you can fill in the number in ' {} ' to specify the position in format (), but if the contents of the key parameter inside ' {} ' are replaced by the value values in format ()

by keyword parameter

in [+]: print ' {name} is a {age} '. Format (name= ' Andy ', age=20) Andy is a 20


by subscript

in []: L = [' Andy ', ' good ']in []: ' {0[0]} {0[1]} {0[0]} '. Format (l) out[60]: ' Andy good Andy '


Format qualifier

It has a rich "format qualifier" (syntax is {} with:), such as:

Fill and align

Padding is used in conjunction with alignment

^, <, > center, Align Left, right, back with width

: The fill character after the number, only one character, not specified by default is filled with a space

Like what

in [+]: ' {: >8} '. Format (' 189 ') out[15]: ' 189 ' in [+]: ' {: 0>8} '. Format (' 189 ') out[16]: ' 00000189 ' in [+]: ' {: a>8 } '. Format (' 189 ') out[17]: ' aaaaa189 '

Accuracy and type F

Accuracy is often used in conjunction with Type F

in []: ' {:. 2f} '. Format (321.33345) out[44]: ' 321.33 '

Where. 2 represents a precision of 2 length, and F represents a float type.

Other types

Mainly in the system, B, D, O, X are binary, decimal, octal, hexadecimal.

In [si]: ' {: b} '. Format (out[54]: ' 10001 ' in []: ' {:d} '. Format (+) out[55]: ' + ' in [+]: ' {: o} '. Format (+) out[56]: ' 21 ' In []: ' {: x} '. Format (out[57]: ' 11 '

Use, the number can also be used to make the amount of thousands separator.

in [+]: ' {:,} '. Format (1234567890) out[47]: ' 1,234,567,890 '


This article is from the "Tridewah operation and maintenance work Road" blog, please be sure to keep this source http://cuidehua.blog.51cto.com/5449828/1770594

Several methods of formatting output mainly introduce the use of the Format function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.