In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use the format () function in Python, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
The call of format function
The format function can be called directly or formatted as a placeholder in the print function.
Prior to the * * 2.6 languages * release, placeholders were still used as% in other languages (for example, in C #). In later versions, however, the placeholder for the format function was changed to {} (curly braces).
The purpose of optimization has been summarized as follows:
1. Curly braces closure can achieve more formatting and more powerful functions.
two。 Make the code more readable.
3. A single parameter can be output multiple times, and the order of the parameters can be different.
4. Regardless of the data type, s can only replace the string type in the% method
All right, let's get into the text, and the use of the format function will be divided into two parts.
The calling format of 1.format function and its placeholder
two。 Placeholder format and parameter setting
Ps: each content will be interspersed slightly during the introduction, in fact, in order to make it easier to understand ~
The calling format of format function and its placeholder
The format function can be called directly, such as in shell.
Example 1
>'{} {} '.format (' hello','world') # placeholders do not specify the order 'hello world' >' {0} {1} '.format (' hello','world') # placeholders make the order 'hello world' >' {1} {0} '.format (' hello','world') # try changing the order 'world hello'
The more common way is to put it in the print () function to format the output
Example 2
Print ('{} won the S8 championship '.format (' IG') # result: # IG won the S8 champion placeholder and parameter setting
In the first part, I briefly introduced two ways to call the format function. It can be clearly found that some parameters can also be entered in the placeholder to set the parameters of the formatted output. Here is a detailed description of the order and settings of the parameters in the placeholder.
The format of the parameters in the placeholder is as follows
Example 3 (pseudo code)
'{position/key:fill,align,sign,width,precision,type}' .format (...) position/key
Position/key specifies which value to call from the parameters of the format function at the placeholder. Position is easy to understand. For example, the last two calls in example 1 above specify the order in which the two parameters hello and world in format are called in the placeholder. Note that this order is calculated from'0' in array sorting. Of course, you can also use key-value pairs to operate. For example, example 4 below:
Example 4
Print ('the championship of the S8 is {S8}, and for S9, it\' {S9}. '.format) # results: the championship of the S8 is IG, and for S9, it'FPX.
Some writing buddies may want to ask, is there any advantage in the use of key-value pairs here? It doesn't feel as fast as filling in a number.
Let's look at the following code:
Example 5:
Dic= ('the championship of the S8 is {S8}, and for S9, it\' {S9}. Format (* * dic)) # results: the championship of the S8 is IG, and for S9, it'FPX.
In example 5, there are some special points that need to be explained. First of all, if you want to call the dictionary in parentheses in format, remember to add * * before the dictionary name, that is, the function call method of the dictionary! As you can see from example 5, we do not need to list dictionaries specifically in format, but can call previously defined dictionaries, which gives it the advantage of repeated calls. Imagine how this will be different, which cannot be done by the% method.
You can also populate it as an array, as shown in example 6:
Example 6:
Names= ['hilary','vergil','nero'] places= [' chengdu','shijiazhuang','tokyo'] print ('Hi, {names [0]}. I am {names [1]} and this is {names [2]}. '.format (names=names) # Hi, hilary. I am vergil and this is nero.
The parameter of the first position is also the only parameter before the colon. I believe you will understand it.
The following parameters are juxtaposed, and we will explain them one by one. First of all, it is necessary to explain clearly how these parameters are juxtaposed or separated.
That is, there is no need to separate, yes, the following parameters, you need to define which to write down in order, without any commas or spaces to separate, you may think, this is not easy to ambiguity? I also thought about this when I was learning, and the fact is that when developers edit function logic, the settings of these parameters are combined with symbols or numbers that do not produce any ambiguity, which you will find when you use it.
Fill parameter
The fill parameter is used to specify the filler, and the default value is a space. The practical experience is that this parameter is used in very few scenarios, unless the international three-digit comma separates the numbers.
Example 7:
> print ('{:,} '.format (12345678)) 12345678align parameter
The align parameter is used for the alignment of formatted text, which is very useful when you fill the width. Of course, if you seamlessly insert a formatted text in a sentence, the setting of this parameter is not very meaningful.
> print ('{: 30} '.format (' pos')) pos > print ('{: ^ 30} '.format (' pos')) possign parameter
The sign parameter is used to specify whether to keep the plus or minus sign, which works for numbers in format.
+-SPACE preserves positive and negative signs, only minus signs, minus signs, minus signs
Example 9:
> print ('{0virtual -} {0:} '.format) + 123-123 > print (' {0-0 -} {0:} '.format (- 123))-123-123-123
Ps: notice that the last one in the first line of code left a space.
Width parameter
The width parameter controls the length of the output. After my tests, the length is the minimum length, that is, when the set width parameter is less than the value called in format, it will not take effect; when the setting value is large, it will be filled with spaces (default) or zeros. If you want to fill it with 0, you need to add the use of zero width parameter before width as shown in example 10. The width parameter is also used when the alignment parameter is shown in example 8, and you can see that the alignment has an impact on the way we fill it, and its specific effect is also given in example 10.
Example 10:
Print ('{0:12} {0:05} {0:2} '.format) # 12-bit wide space filling, 5-bit width 0 filling, 2-bit width # result: # 123 00123 123print (' {0VO12}, {0: ^ 012} '.format (123)) # result: # 12300000000000000000000000123000012300000 print (' {0: ^ 012}, {1: ^ 012}, {0: ^ 011}, {1: ^ 011} '.format (123prime1234)) # result: # 000012300000work000012340000and1230000000012300000000123000000001230000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
It can be seen that the filling of spaces is also shown by the difference of alignment, but the filling of 0 often changes the representation of our numbers, so we should pay attention to it when using it. Another thing to note is that when the parity of your total width is different from the effective width of the data, the alignment in the center will be different, which is what the third line of code in example 10 wants to say. It can be seen that when the odd and even times are different, the alignment is always skewed to the left.
Precision parameter
As in the% method, such as% .3f means to retain three decimal places, using [. Keep the number of significant digits (f)] indicates the accuracy of the data, if f is added, it indicates that the number of decimal places is retained, and if it is not added, it means that the number of significant digits is retained. Example 11 shows this difference very well.
Example 11:
Print ('{0vir. 2f}, {0v. 7f}, {0v. 2}, {0. 2%} '.format (123.123456789)) # result: # 123.123.1234568, 1.2eShut02jue 12312.35%
In fact, my friends should have noticed that the fjord% here is actually the content controlled by the type parameter after the precision parameter-the data type. Therefore, we briefly introduce the accuracy, and introduce the joint parameters of the precision-type parameters in detail, because the two have a strong relationship.
Precision-type parameter
First, all the type parameters are introduced, as shown in the following table:
Type parameter meaning example defaults to decimal integer 123f floating point number 123.123%
Percentage format 12312.3% e
Exponential form 1.2e01-d decimal integer 123 decimal conversion decimal 123b binary 1101111o octal 157x hexadecimal 6f#x lowercase hexadecimal 0x6f#X uppercase hexadecimal 0X6Fc characters, converted to Unicode code '123' before printing is all the contents of this article "how to use the format () function in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.