奶茶mm被高帅富破了,恋恋笔记本好看吗,000511资金流向
similar to strings numbers can also be constrained to a specific width.
'%4d' % (42,)
'{:4d}'.format(42)
42
again similar to truncating strings the precision for floating point numbers limits the number of positions after the decimal point.
for floating points the padding value represents the length of the complete output. in the example below we want our output to have at least 6 characters with 2 after the decimal point.
'%06.2f' % (3.141592653589793,)
'{:06.2f}'.format(3.141592653589793)
003.14
for integer values providing a precision doesn't make much sense and is actually forbidden in the new style (it will result in a valueerror).
'%04d' % (42,)
'{:04d}'.format(42)
0042
by default only negative numbers are prefixed with a sign. this can be changed of course.
'%+d' % (42,)
'{:+d}'.format(42)
+42
use a space character to indicate that negative numbers should be prefixed with a minus symbol and a leading space should be used for positive ones.
'% d' % ((- 23),)
'{: d}'.format((- 23))
-23
'% d' % (42,)
'{: d}'.format(42)
42
new style formatting is also able to control the position of the sign symbol relative to the padding.
this operation is not available with old-style formatting.
'{:=5d}'.format((- 23))
- 23
'{:=+5d}'.format(23)
+ 23
both formatting styles support named placeholders.
data = {'first': 'hodor', 'last': 'hodor!'}
'%(first)s %(last)s' % data
'{first} {last}'.format(**data)
hodor hodor!
.format()
also accepts keyword arguments.
this operation is not available with old-style formatting.
'{first} {last}'.format(first='hodor', last='hodor!')
hodor hodor!
new style formatting allows even greater flexibility in accessing nested data structures.
it supports accessing containers that support __getitem__
like for example dictionaries and lists:
this operation is not available with old-style formatting.
person = {'first': 'jean-luc', 'last': 'picard'}
'{p[first]} {p[last]}'.format(p=person)
jean-luc picard
data = [4, 8, 15, 16, 23, 42]
'{d[4]} {d[5]}'.format(d=data)
23 42
as well as accessing attributes on objects via getattr()
:
this operation is not available with old-style formatting.
class plant(object):
type = 'tree'
'{p.type}'.format(p=plant())
tree
both type of access can be freely mixed and arbitrarily nested:
this operation is not available with old-style formatting.
class plant(object):
type = 'tree'
kinds = [{'name': 'oak'}, {'name': 'maple'}]
'{p.type}: {p.kinds[0][name]}'.format(p=plant())
tree: oak
new style formatting also allows objects to control their own rendering. this for example allows datetime objects to be formatted inline:
this operation is not available with old-style formatting.
from datetime import datetime
'{:%y-%m-%d %h:%m}'.format(datetime(2001, 2, 3, 4, 5))
2001-02-03 04:05
additionally, new style formatting allows all of the components of the format to be specified dynamically using parametrization. parametrized formats are nested expressions in braces that can appear anywhere in the parent format after the colon.
old style formatting also supports some parametrization but is much more limited. namely it only allows parametrization of the width and precision of the output.
parametrized alignment and width:
this operation is not available with old-style formatting.
'{:{align}{width}}'.format('test', align='^', width='10')
test
parametrized precision:
'%.*s = %.*f' % (3, 'gibberish', 3, 2.7182)
'{:.{prec}} = {:.{prec}f}'.format('gibberish', 2.7182, prec=3)
gib = 2.718
width and precision:
'%*.*f' % (5, 2, 2.7182)
'{:{width}.{prec}f}'.format(2.7182, width=5, prec=2)
2.72
the nested format can be used to replace any part of the format spec, so the precision example above could be rewritten as:
this operation is not available with old-style formatting.
'{:{prec}} = {:{prec}}'.format('gibberish', 2.7182, prec='.3')
gib = 2.72
the components of a date-time can be set separately:
this operation is not available with old-style formatting.
from datetime import datetime
dt = datetime(2001, 2, 3, 4, 5)
'{:{dfmt} {tfmt}}'.format(dt, dfmt='%y-%m-%d', tfmt='%h:%m')
2001-02-03 04:05
the nested formats can be positional arguments. position depends on the order of the opening curly braces:
this operation is not available with old-style formatting.
'{:{}{}{}.{}}'.format(2.7182818284, '>', '+', 10, 3)
+2.72
and of course keyword arguments can be added to the mix as before:
this operation is not available with old-style formatting.
'{:{}{sign}{}.{}}'.format(2.7182818284, '>', 10, 3, sign='+')
+2.72
网址是https://pyformat.info/#number_padding
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python爬虫:Request Payload和Form Data的简单区别说明
浅谈Python中threading join和setDaemon用法及区别说明
Python3-异步进程回调函数(callback())介绍
python继承threading.Thread实现有返回值的子类实例
Python中使用threading.Event协调线程的运行详解
网友评论