Python----format

Source: Internet
Author: User
Tags natural string

1/cmd command under Python Development Mode Exit method: Ctrl + Z

2/natural string preceded by the string R, for example R "hello/n", you can retain the escape character

3/String Repeat *n

4/SUBSTRING: index, slice

5. Set: Establish relationship (& intersection, | Set,-difference set), remove duplicate elements

6. Pickle Pickling Purpose: Data is serialized, stored in memory, and persisted locally

Methods: 1, pickle module: Dumps function to serialize data, loads function to restore data, the data type is restored

2, stored in the third file serialization, dump function to store objects in the file

F1=file (' 1.PK1 ', ' WB ')
Pickle.dump (Object,file,true)

The load function restores the data stored in the file

F2=file (' 1.pk1 ', ' RB ')
T=pickle.load (F2)

7,//Divide the whole number of parts

* * Exponentiation operation

~ Bitwise rollover ~a=-(A+1)

The following transfers from http://www.jb51.net/article/63672.htm

The function Str.format () that formats the string,

Grammar

It replaces% with {} and:.
Map sample

By location

?
123456 In [1]: ‘{0},{1}‘.format(‘kzc‘,18) Out[1]: ‘kzc,18‘ In [2]: ‘{},{}‘.format(‘kzc‘,18) Out[2]: ‘kzc,18‘ In [3]: ‘{1},{0},{1}‘.format(‘kzc‘,18) Out[3]: ‘18,kzc,18‘

The Format function of the string can accept an unlimited number of parameters, the position can be out of order, can not be used or multiple times, but 2.6 can not be empty {},2.7.
by keyword parameter

?
12 In [5]: ‘{name},{age}‘.format(age=18,name=‘kzc‘) Out[5]: ‘kzc,18‘

Through object properties

?
12345 class Person: def __init__(self,name,age): self.name,self.age = name,age def __str__(self): return ‘This guy is {self.name},is {self.age} old‘.format(self=self)
?
12 In [2]: str(Person(‘kzc‘,18)) Out[2]: ‘This guy is kzc,is 18 old‘

by subscript

?
123 In [7]: p=[‘kzc‘,18] In [8]: ‘{0[0]},{0[1]}‘.format(p) Out[8]: ‘kzc,18‘

With these convenient "mapping" methods, we have a lazy weapon. Basic Python knowledge tells us that list and tuple can be "broken" into normal parameters to the function, and dict can be broken into the keyword parameters to the function (through and *). So you can easily pass a list/tuple/dict to the Format function. Very flexible.
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

?
123456 In [15]: ‘{:>8}‘.format(‘189‘) Out[15]: ‘ 189‘In [16]: ‘{:0>8}‘.format(‘189‘) Out[16]: ‘00000189‘In [17]: ‘{:a>8}‘.format(‘189‘) Out[17]: ‘aaaaa189‘

Accuracy and type F
Accuracy is often used in conjunction with Type F

?
12 In [44]: ‘{:.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.

?
12345678 In [54]: ‘{:b}‘.format(17) Out[54]: ‘10001‘In [55]: ‘{:d}‘.format(17) Out[55]: ‘17‘In [56]: ‘{:o}‘.format(17) Out[56]: ‘21‘In [57]: ‘{:x}‘.format(17) Out[57]: ‘11‘

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

?
12 In [47]: ‘{:,}‘.format(1234567890) Out[47]: ‘1,234,567,890‘

Python----format

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.