Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use format in python

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use format in python". In daily operation, I believe many people have doubts about how to use format in python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use format in python". Next, please follow the editor to study!

The use of format function in format in python

This is a method of string formatting, such as str.format ().

The basic syntax is to replace the previous% through {} and:.

The following two main uses are shown:

(1) for example: statement print ("{: .2f}" .format (3.1415926)), its output is 3.14, you can see that the command keeps two decimal places.

(2) for example: statement "{1} {0} {1}" .format ("hello", "world"), its output is' world hello world', you can see that format has set the location for them.

Another example: the statement print'{} * '.format (j) represents the value of the output variable j and the multiplication sign: Jake, you can see the equation that can be used in the loop to output a multiplication.

Another example: the position of the statement print "{}" is {{0}} ".format (" runoob "), and its output is' runoob.

The following table shows several ways for str.format () to format numbers:

Format format output case explanation a = "Python Rank examination" b = "=" c = ">" print ("{0: {1} {3} {2}}" .format (a less than 25 pr c))

Output result: = Python grade exam

explain

1. Format format specifier General format {[serial number or key]: format specifier}

2. 0 is the sequence number of the output, pointing to the position of the output object in the format () function, that is, pointing to a

3. {1} {3} {2} is the format specifier as a whole, the separate {1} points to the content b of the second position in the format function, the separate {3} points to the content c of the fourth position in the format function, and the separate {2} points to the content 25 of the third position in the format function. Put these three together to form a complete format specifier "= 25 >". Align the output to the right and fill the free position with "=" so that the output occupies 25 bytes.

4. The output an occupies only 10 bytes and needs to be filled with 15 bytes of "=" to make the content meet the requirements of the format specifier.

5. According to the output of the above four points, the output result is obtained.

At this point, the study of "how to use format in python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report