In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "the process of Python formatting string output". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the process of Python formatting string output.
A brief introduction
There are two ways to format python string output. Python 2.6 provides string.format (), which is also quite powerful. Talk is cheap,show me the code.
Second use
2.1 Parameter mapping
Str.format replaces% of the string through {}, and we can use location-based mapping parameters, based on the following table, based on parameters
such as
In [23]: print'i am a% s work at% s!'% ('dba','youzan')
I am a dba,work at youzan!
In [24]: print'i am a {0}, work at {1}! '.format (' dba','youzan')
I am a dba,work at youzan!
In [26]: print'i am a {arg}, work at {company}! '.format (arg='dba',company='youzan')
I am a dba,work at youzan!
Format does not limit the number of calls to parameters.
In [28]: print'i am a {0}, work at {1}, and {1} is good at SAAS service! '.format (' dba','youzan')
I am a dba,work at youzan,and youzan is good at SAAS service!
2.2 formatted output
% provides rich formatting output, and format certainly has the same functionality.
Fill and align
^ Center
< 左对齐 >Align the rear band width to the right
The character with padding after the: sign can only be one character. If not specified, it is filled with spaces by default.
The specific ways of use are as follows
In [30]: fs=' {: 8}'
In [35]: fs.format ('dba')
Out [35]: '11111dba'
# Center
In [36]: fs=' {: 1 ^ 8}'
In [37]: fs.format ('dba')
Out [37]: '11dba111'
Floating point precision
In [40]: fs=' {: .3f}'
In [41]: fs.format (3.14159265358)
Out [41]: '3.142'
The system of numbers
B is binary respectively.
D decimal system
O Octal
X hexadecimal.
In [42]:': b'.format (29)
Out [42]:': B'
In [43]:'{: B} '.format (29)
Out [43]: '11101'
In [44]:'{: d} '.format (29)
Out [44]: '29'
In [45]:'{: X} '.format (29)
Out [45]: '1d'
In [46]:'{: o} '.format (29)
Out [46]: '35'
A comma can also be used as a thousand-digit separator for the amount.
In [47]:'{:,} '.format (2132323455)
Out [47]: '2132323455'
At this point, I believe that everyone on the "Python format string output process" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.