The python .format

A, Python% or format should be used to format string?

The new version of Python recommended format.

Python2.6 newly added format syntax support.

3.6 added a new feature f-strings

1, the difference

Python string formatting There are two camps:% and format, what should we choose?

Since the introduction of the format Python2.6 this method after the format string, I think% or format this is simply not a problem.

# Define a coordinate value

c =(250, 250)

#% Use to format

s1 = "enemy coordinates:% s"% c

Obviously the above code will throw a following TypeError:

TypeError: not all arguments converted during string formatting

As the demand for such formatted we need to write the following format for the job:

# Define a coordinate value

c =(250, 250)

Ugly formatting using the% # ...

s1 = "enemy coordinates:% s"% (c,)

And uses the format would not exist the above questions:

# Define a coordinate value

c =(250, 250)

# Use format to format

s2 = "enemy coordinates: {}". format (c)

Obviously, format more convenient than a%.

2, New Features

Python3.6 added in the F-strings :

In[1]: name ="Q1mi"

In[2]: age =18

In[3]: f"My name is {name}.I'm {age}"

Out[3]: "My name is Q1mi.I'm 18"

Second, the use of commonly used format

1, by position

In[1]: data =["Q1mi", 18]

In[2]: "Name:{0}, Age:{1}".format(*data)

Out[2]: 'Name:Q1mi, Age:18'

2, keyword

In[1]: data ={"name": "Q1mi", "age": 18}

In[2]: "Name:{name}, Age:{age}".format(**data)

Out[2]: 'Name:Q1mi, Age:18'

3, object properties

In[1]: classPerson(object):

   ...:     def__init__(self, name, age):

   ...:         self.name =name

   ...:         self.age =age

   ...:     def__str__(self):     

   ...:         return"This guy is {self.name}, {self.age} years old.".format(self=self)

   ...:    

In[2]: p =Person("Q1mi", 18)

In[3]: str(p)

Out[3]: 'This guy is Q1mi, 18 years old.'

4, by the subscript

In[1]: "{0[0]} is {0[1]} years old.".format(data)

Out[1]: 'Q1mi is 18 years old.'

5, aligned with the filling

Often used in conjunction with aligned filling

^, <,> Are centered, left aligned, right aligned, the width of the back

: Back-filled with a number of characters, only one character is not specified, the default is filled with spaces.

In[1]: "{:>10}".format('18')

Out[1]: '        18'

In[2]: "{:0>10}".format('18')

Out[2]: '0000000018'

In[3]: "{:A>10}".format('18')

Out[3]: 'AAAAAAAA18

A supplementary string comes zfill () Method:

Python zfill () method returns a specified length of the original string right justified, padded with zeros in front.

zfill () method syntax: str.zfill (width)

Parameters width length of the string. Original string right justified, padded with zeros in front.

Returns a string of specified length.

In[1]: "18".zfill(10)

Out[1]: '0000000018'

6, the accuracy of the type f

Accuracy often used in conjunction with type f.

In[1]: "{:.2f}".format(3.1415926)

Out[1]: '3.14'

Wherein an accuracy of .2 represents the length of 2, f denotes a float.

7. Other hex

The main band is a, b, d, o, x are binary, decimal, octal, hexadecimal.

In[1]: "{:b}".format(18)

Out[1]: '10010'

In[2]: "{:d}".format(18)

Out[2]: '18'

In[3]: "{:o}".format(18)

Out[3]: '22'

In[4]: "{:x}".format(18)

Out[4]: '12'

8, thousands separator

In[1]: "{:,}".format(1234567890)

Out[1]: '1,234,567,890'

Guess you like

Origin www.cnblogs.com/waterstar/p/11320921.html