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

The process of Python formatting string output

2025-01-18 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.

Share To

Database

Wechat

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

12
Report