Foundations Python string operations, format.

Python strings now feel much in everyday use, but after carefully read the book a few tricks still very convenient, simple record under me.

Insert a super-lattice forced out:

.format_map (In fact, this method is a string of .....)

 

In [83]: info = {"name": "sidian", "age": 18}                                                         

In [84]: "my name is {name},age is {age}".format_map(info)                                            
Out[84]: 'my name is sidian,age is 18'

 

 When you do a data table of key-value pairs, not formatted output Shuangsi

Cued ~

First came a high force grid:

>>> name='sidian'
>>> f'my name is {name}'
'my name is sidian'

 This effect with 'my name is {name}'. Format (name = 'sidian') the same, but I think I wrote the above wording, others see the code I definitely special equipment 13

>>> names = ['yidian', 'erdian', 'sandian']
>>> 'you name is {name[1]}'.format(name=names)
'you name is erdian'

 The wording written inside a key wording, keywords inside the value behind the keyword arguments replication list, feeling like a general degree of convenience

Not as a direct write

>>> 'you name is {name}'.format(name=names[1])
'you name is erdian'

 Such logic more clearly, so anyway, I feel.

 

Thousands separator, this is interesting.

>>> 'num is {:,}'.format(1000**10)
'num is 1,000,000,000,000,000,000,000,000,000,000'

 This can be seen in the past more comfortable in the output of large numbers of people

The key here this: that the left of the colon fill out index keys (0 or 1 or 2), the right of the colon to fill out some forms and output format (: 0.2f or: 05.3f)

Ten million kinds of tricks on the right, I pick the next book showcase:

>>> from math import pi
>>> '{:010.2f}'.format(pi)
'0000003.14'
>>> 

 This is better understood, the left did not write default order parameter, the first right number 0 is filled, the second digital number 10 is 10 bits, the third valid decimal number 2 is 2, f is the last data of type float.

>>> '{:<0.2f}'.format(pi)
'3.14'
>>> '{:<10.2f}'.format(pi)
'3.14      '
>>> '{:^10.2f}'.format(pi)
'   3.14   '
>>> '{:>10.2f}'.format(pi)
'      3.14'

This can be seen by: <^> are aligned behind the left, center, right, front after the number mean already explained.

>>> '{:$^20}'.format('ok')
'$$$$$$$$$ok$$$$$$$$$'
>>> 

 Text formatting characters: The first one is the right filling number, the middle symbol for the second, third to the string length

>>> print('{0:10.2f}\n{1:&=10.2f}'.format(pi,-pi))
      3.14
-&&&&&3.14
>>> print('{0:10.2f}\n{1:=10.2f}'.format(pi,-pi))
      3.14
-     3.14
>>> print('{0:10.2f}\n{1:10.2f}'.format(pi,-pi))
      3.14
     -3.14

 There are three outputs, respectively, from the simple to the complex, the last one is actually very simple, is the location of the output format, then two parameters, but a negative number programmed

Second: on the right there is a equal, this is interesting, it allows to produce a space between symbols and numbers, the default is a space.

The first one: on the right there is a filling element & so between symbols and numbers can define their own different symbols.

>>> print('{0:10.2f}\n{1:&<10.2f}'.format(pi,-pi))
      3.14
-3.14&&&&&
>>> print('{0:10.2f}\n{1:&^10.2f}'.format(pi,-pi))
      3.14
&&-3.14&&&

 In fact, in front of the digital center, left justified, right justified, you can also add padding element, the default padding element spaces.

 

Here, then explain how to format the output + - Number:

In [6]: '{0:-.2}'.format(pi)                                                                          
Out[6]: '3.1'

In [7]: '{0:-.2f}'.format(pi)                                                                         
Out[7]: '3.14'

In [8]: '{0:+.2}'.format(pi)                                                                          
Out[8]: '+3.1'

In [9]: '{0:-.2}'.format(pi)                                                                          
Out[9]: '3.1'

In [10]: '{0:-.2}'.format(-pi)                                                                        
Out[10]: '-3.1'

In [11]: '{0:+.2}'.screen (-pi)                                                                        
Out[11]: '-3.1'

In [12]: '{:^+10.2}'.format(pi)                                                                       
Out[12]: '   +3.1   '

 As can be seen by the above operation:

: The default is actually the right - the actual operation if you want to add a plus sign, you can: add the right + right if the right in its symbol should be aligned symbols (<, ^>) of;

According to previous test so much, I give myself summed up the basic right of the colon is filled with the first symbol (default is a space)

As its second symbol (<, ^>)

The third aa is a string of aa.b, b is the actual accuracy.

In [18]: '{:8.2}'.format(5800)                                                                        
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-e85d3e6ac999> in <module>
----> 1 '{:8.2}'.format(5800)

ValueError: Precision not allowed in integer format specifier

In [19]: '{:8.2}'.format(3.985)                                                                       
Out[19]: '     4.0'

'{:8.2}'.format('做人没意思')                                                                
Out[20]: '做人      '

 From the above test digital default alignment is right-aligned, the string is left-aligned, but also engage in such trouble

And this way can not enter an integer format

 

If the fourth {: 5.2f} such cases, is described taking 5 values, two after the decimal point taken.

In [22]: '{:8.2f}'.format(3.1415926)                                                                  
Out[22]: '    3.14'

 

{Output} format:

In [26]: '{{:{}}}'.format(10)                                                                         
Out[26]: '{:10}'

 

And this way can not enter an integer format
[Bright zhǒng Fang Zhe Geshan Hua Shura zhěngshù the Works]
And this way can not enter an integer format

Guess you like

Origin www.cnblogs.com/sidianok/p/11780399.html