%
(s % d) , s , d (). sprintf() C.
: ( ) , . d , d. d , ( , ). % , . .1.
.1.
d, i | . |
u | . |
o | . |
x | . |
X | ( ). |
f | [-]m.dddddd. |
e | [-]m.dddddde xx. |
E | [-]m.ddddddE xx. |
g, G | %e %E, -4 ; %f. |
s | . str(). |
r | , repr(). |
c | . |
% | %. |
% :
- , . , KeyError.
- :
- -, . .
- +, ( ).
- 0, .
- , . , , ( , ) .
- , .
- , ; , , , .
, (*). , .
|
|
:
stock = {
'name': 'GOOG',
'shares': 100,
'price':490.10 }
r = '%(shares)d %(name)s %(price)0.2f' % stock
print (r)
, . vars() , , .
name = ''
age = 41
r = '%(name)s, %(age)s ' % vars()
print (r)
str.format()
str.format() , . ( str , )
str.format() , {}, . :
"The novel '{0}" was published in {1}"'.format("Hard Times", 1854)
"The novel 'Hard Times' was published in 1854"
, , str. format(). 0 , 1 - .
:
{}
{!}
{:}
{!:}
, . :
"The {who} was {0} last week".format(12, who="boy")
:
'The boy was 12 last week'
. ( !):
>>> = ["50", "40", "42", "507", "28,5/11"]
>>>" {0[1]} {0[2]} V- - .".format()
:
' 50 40 V- - .'
.
, math sys:
>>>" {0.pi}. {1.maxunicode}".format(math, sys)
:
' 3.14159265359. 65535'
Python (), str.format() . str.format() . , .
:
>>>'{0:*<15}".format(18340427) # * -, , 15
'18340427*******'
>>>"{0:*>15}".format(18340427) # * -, , 15
'*******18340427'
|
|
>>>"{0:*~15}".format(18340427) # * -, , 15
'***18340427****'
>>>"{0:*~15}".format(-18340427) # * -, , 15
'***-18340427***'
:
>>>"[{0: }] [{1: }]".format(539802, -539802) # "-"
'[ 539802] [-539802]'
>>>"[{0:+}] [{1:+}]".format(539802, -539802) #
'[+539802] [-539802]'
>>>"[{0:-}] [{1:-}]".format(539802, -539802) # "-"
'[539802] [-539802]'
:
>>>"{0:b} {0:o} {0:x} {0:X}".format(14613198)
'110111101111101011001110 67575316 deface DEFACE'
>>>"{0:#b} {0:#o} {0:#x} {0:#X}".format(14613198)
'0b110111101111101011001110 0o67575316 0xdeface 0XDEFACE'
, , .
- n. d g . n , , - . ѻ, , - .
, :
import locale
locale.setlocale(locale.LC_ALL, "")
, (, LANG) ѻ .
:
x,y = (1234567890, 1234.56)
locale.setlocale(locale.LC_ALL, "C")
c = "{0:n} {1:n}".format(x, y) #
print (c)
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
en = "{0:n} {1:n}".format(x, y) #
print (en)
:
12345678901234.56
1,234,567,8901,234.56
n , , .
, , . , .
:
Ż - ,
f - ,
g ( G) - , f, - ( E),
% - :
>>>amount = (10** 3) * math.pi
>>>"[{0:12.2}] [{0:12.2f>]".format(amount)
'[ 3.14e+03] [ 3141.59]'
>>>"[{0:*>12.2}] [{012.2f}]". format(amount)
'[****3.14e+03] 0****3141.59]'
>>>"[{0:*>+12.2}] [{0:*>+l2.2f}]".format(amount)
'[***+3.14e+03] [****+3141.59]'
12 2 . - *. - , ( , ). , + .
|
|