str.format() formatting method
The% percentile formatting method mentioned earlier. Python officials have recently gradually promoted the formatting of str.format() method.
The fromat() method is a built-in method for strings. Its basic syntax is as follows:
Parameter list:[[fill]align][sign][#][0][width][,][.precision][type] fill [Optional] characters filled in the blank space align [Optional] alignment (to be matched) width (use) <,Align content left >,Align content right(default) =,Align the content to the right, place the symbol to the left of the padding character, and is valid only for numeric types. Even if: Symbols+Filler+number ^,Content centered sign [[optional] unsigned number # [Optional] for binary, octal and hexadecimal, if you add#, 0b/0o/0x will be displayed, otherwise it will not be displayed +,Plus plus, minus plus minus; -,The positive sign remains unchanged, and the negative sign plus minus; Space, plus sign, space, minus sign plus minus; , [Optional] add a separator for the number, such as: 1,000,000 width [[optional] width of format bit .precision [[optional] decimal place retention precision type [[optional] format type Pass in parameters of string type: s,Format string type data Blank, if no type is specified, the default is None,with s Parameters passed in integer type: b,Automatically convert a decimal integer to a binary representation and format it c,Automatically converts a decimal integer to its corresponding unicode character d,Decimal integer o,The decimal integer is automatically converted to the octal representation, and then formatted; x,Automatically convert a decimal integer to a hexadecimal representation and format it (lowercase) x) X,Automatically convert a decimal integer to a hexadecimal representation and format it (uppercase X) Parameters passed in floating point or decimal type: e, Convert to scientific notation (lowercase) e)Represent, and then format; E, Convert to scientific notation (in uppercase) E)Represent, and then format; f , Convert to floating-point (6 digits after the default decimal point) representation, and then format; F, Convert to floating-point (6 digits after the default decimal point) representation, and then format; g, Automatically in e and f Medium switching G, Automatically in E and F Medium switching %,Display percentage (6 decimal places by default)
There are many parameters and usages, which make me dizzy. It is obviously unnecessary to write them down, wasting brain cells.
In addition to complex parameters, there are basically two types of simple format methods:
1. {0}, {1}, {2}: These are location parameters. The references must be in order and cannot be adjusted at will, otherwise they will be disordered. For example:
tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
2.{name}, {age}, {gender}: These are keyword parameters, which must be referenced in the form of key value pairs, and the order can be adjusted at will. For example:
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
Here are some specific examples for reference:
tpl = "i am {}, age {}, {}".format("seven", 18, 'alex') tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex']) tpl = "i am {0}, age {1}, really {0}".format("seven", 18) tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18]) tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18) tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33]) tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1) tpl = "i am {:s}, age {:d}".format(*["seven", 18]) tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18) tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18}) tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15) tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
For more usage, please refer to the official documentation https://docs.python.org/3/library/string.html
Character encoding
The computer can only process the number 01. If you want to process the text, you must first convert the text to the number 01. This conversion method is called character coding.
For us, you just need to remember the following codes:
ASCII coding: in the early days, it was specially coded for the English language family, with only 255 characters. Each character needs 8 bits, that is, 1 byte. It is incompatible with Chinese characters.
Unicode code: also known as universal code, it is a character coding scheme formulated by international organizations that can accommodate all characters and symbols in the world. It uses 2 bytes to represent Chinese characters.
UTF-8 encoding: in order to save bytes, it is optimized on the basis of Unicode. One byte represents English characters and three characters represent Chinese characters. It is naturally compatible with ASCII encoding, so it is the most popular.
GB2312: Chinese code developed by China in the early stage, which is not common all over the world.
GBK: full name: Chinese character internal code extension specification. It is compatible with GB2312 downward and supports ISO10646.1 international standard upward. It is a product of the transition from the former to the latter. GBK is used for Chinese character coding in windows Chinese version. It is not a universal coding in the world
Other codes: a general term for those not of the above types. They belong to codes that can be used without touching.
Last but not least, python 3 uses Unicode encoding at runtime!
In addition, there are several rules you should remember:
When the operating system is running, Unicode coding is uniformly used in the memory. When it is necessary to save the data to the hard disk or network transmission, it is converted to UTF-8 coding for saving and transmission.
When using a text editor, the UTF-8 encoded characters read from the file system or hard disk are converted into Unicode characters and stored in memory for use by programs or operating systems. After editing, when saving, convert Unicode into UTF-8 and save to a file.
When browsing the web, the server will convert the dynamically generated Unicode content into UTF-8 and transmit it to the client's browser.
This is why we use Unicode and utf-8.
In addition, be sure to distinguish between the code itself and the data to be processed by the program! If there are no special requirements, please use utf-8 coding. Ensure that the text editor uses UTF-8 without BOM coding.
So far, two sets of Python basic strength courses are specially recommended, which are created by the IT Research Institute of public education and combined with the practical experience of front-line large factories, and polished two sets of Python online basic courses. Challenge the zero foundation introduction of Python for 3 days to cultivate your python programming practical ability.
For course details, please click the text link below ↓↓↓