In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to format the output of Python, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Output of python
The print () function in the python output can be referenced directly. Its complete format is as follows:
# there are file operations at the end, and we mainly learn about output, so we won't join print (object, sep='', end='\ n') for the time being.
Object-- is what we want to output. It can be a variable or specific data. It can be single or multiple. If it is multiple, it can be separated by','.
Sep-- is only used when we want to output multiple things. It indicates what is used to separate the output of multiple data. If the output is not set, it is separated by a space by default.
End-- is used to set what to end with after outputting the data we want to output. When you don't set it deliberately, it defaults to end='\ n', so the print () function automatically wraps lines after the default output.
Print (1, 2, 3, print) print (1, 2, 2, 3) print (1, 2, 3,) # their output is as follows: 1 2-3-1-2-3-12, please think carefully about why, and sort things out.
Our scanf () function in C language is formatted, and you can set the format and form of the data to be output, so how do we implement it in python?
Int axiom 10 float baud 6.6 umbrellas scanf ("% d% f\ n", ameme b)
The above C code is implemented in python as follows:
A=10b=6.6print ("% d% f"%% (aMaginb)) # print (arecalogical bregory septicals')
If we are required to choose the above method for the output data format, we can modify the output format as well as the c language.
If we are not required to choose the second method for the output format, it is convenient to use + to concatenate when outputting with a string, but only if the output type is also a string type. If it is not a string type, then we can use the str () function to cast the connection output as follows:
A=18print ("Her age is" + a + "!")
It is wrong to write like this, because an is assigned to 18, so the data type of an is integer, and the integer cannot be directly connected with the string with +, so the correct way to write it is:
A=18print ("Her age is" + str (a) + "!") # output is: Her age is 18!
Next, let's talk about formatting output data.% of the formatted output is more or less the same as that of C language:
% dmurt-decimal integer
% o-Octal integer
% x-hexadecimal integer
% ftel-floating point, six significant digits after the decimal point
% e-exponential output, six significant digits after the decimal point
Under the premise of% g-six significant digits, use the decimal method, otherwise use the scientific counting method
A=5print (10) print ("% d"% a) print ("% 2d"% a) # output according to 2 space size, default is right-aligned print ("%-2d"% a) # 2 space size, because d is preceded by a-sign, left align print ("d"% a) # 2 space size output, insufficient 0 to make up print ("%-02d"% a) # 2 space size output, insufficient 0 to make up, left alignment
The output of the above code is:
ten
five
five
five
05
five
To sum up the above code, you can add some numbers or symbols between% and d to control the format of the output data, specifically:
% (control left and right alignment) (what to make up) (output according to a few bits of space) d is:
The default is right alignment, and the negative sign is left alignment
Default space completion, you can set other completion
When the number itself exceeds the required space, there is no impact, and when the data itself is not enough, according to the left and right alignment, according to the make-up. To put it bluntly, it is to occupy a seat.
Other data types are similar, whether floating-point numbers or strings, such as% 5.2f, occupy five places and retain two decimal places. It is important to note that floating-point decimal points also occupy one place. Next, take a look at a few examples and have a good experience.
B=5.21print (b) print ("f" b) print ("2.1f" b) print ("2.2f" b) print ("5.2f" b) print ("5.5f" b) print (".5f" b)
# the output above is as follows:
5.21
5.210000
5.2
5.21
5.21
5.21000
5.21000
# pay attention to the comparative study
Some things must be realized before they can be written down. I hope everyone can think diligently and don't be lazy!
Next, we introduce a function-- str.format ().
Format () is more powerful than the basic formatted output, which treats the string as a template, formats it with passed parameters, and uses curly braces'{}'as a special character instead of'%'.
Don't talk too much nonsense and put on the code!
# through position matching, similar to array print ('{} {} '.format (' hello','baibai')) # hello baibaiprint ('{1} {0} '.format (' hello','baibai')) # baibai helloprint ('{1} {1} '.format (' hello','baibai')) # baibai baibaiprint ('{0} {0} {0} 'format (' hello','baibai')) # hello hellohello# by matching the corresponding value names Similar to the dictionary # (you can also pass in a dictionary type for key to correspond to value) print ('{a}, {b} '.format
In addition to these, we can also output more complex data types
# or use one of the data I defined earlier, goods= [(1, Pepsi, 10Magne3), (2, "Kang Shifu Instant Noodle", 15Power2), (3, "Mug", 5Magazine 30)] list= [1] print ('{0 [1]} '.format (goods)) # (2,' Kang Shifu Instant Noodle', 15) print ('{0 [1]}, {1 [2]} '.format (goods,list)) # (2) 'Master Kang instant noodles', 15,2), 3print ('{0 [1] [1]} '.format (goods,list)) # thank you for reading this article carefully I hope the article "how to format the output of Python" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.