Python programming inside%, "% s and% d" representative of the meaning of
% s, representation format of formula a object character
% d, the integer
"the Hello,% S"% "zhang3" => "the Hello, zhang3"
"% D"% 33 is => "33 is"
"% S:% D" % ( "ab &",. 3) => "ab &:. 3"
% characters: conversion described start tag breaks.
Placing a string (format string)% of the left and the right value is formatted desired placement.
% s indicates the formatting rules
1、
'%s plus %s equals %s' % (1,2,2)
Out[29]: '1 plus 2 equals 2'
2、
'Price of eggs: $%d' % 42
Out[30]: 'Price of eggs: $42'
3、
单独使用时取余5%3
5%3
Out[28]: 2/4
4、
LOTTERY_PRE = "LXG_LOT_"
LOTTERY_ITEM = LOTTERY_PRE + '%s_ITEM'
new_version = "20181007220245756"
new_lobbery_item = LOTTERY_ITEM % new_version
print(new_lobbery_item)
输出 LXG_LOT_20181007220245756_ITEM
Usage in Python print
% s string
- string="hello"
When the printing result is hello% s
- print "string=%s" % string # output: string=hello
% 2s mean length of the string 2, when the length of the string is more than 2, the original length of the printing, the print result or% 2s hello
- print "string=%2s" % string # output: string=hello
% 7s mean length of the string 7, when the length of the string is smaller than 7, the left side of the original string fill space,
So the print result is hello% 7s
- print "string=%7s" % string # output: string= hello
% -7s mean length of the string 7, when the length of the string is less than 7, to the right of the original string fill space,
So the print result is hello% -7s
- print "string=%-7s!" % string # output: string=hello !
% .2s means intercepts the first two characters of the string, the print result is he% .2s
- print "string=%.2s" % string # output: string=he
% .7s means intercepts the first 7 characters of the string, the string length is less than when the original 7, that is, the string itself,
So the print result is hello% .7s
- print "string=%.7s" % string # output: string=hello
% A.bs This format is an integrated two formats above, according to the first taken after the decimal point character string number B,
When taken string length is less than a, which also need to fill a space left
- print "string=%7.2s" % string # output: string= he
- print "string=%2.7s" % string # output: string=hello
- print "string=%10.7s" % string # output: string= hello
- # Can also use% . S to represent precision, the two values * parentheses after the first two bits of the specified value
- print "string=%.s" % (7,2,string) # output: string= he
%d 整型 - a = 14
% D 14 is the print result
- print "num=%d" % num # output: num=14
% 1d means print result is an integer of 1, when more than one digit integer, integer press printing original value, so the print result or 14% 1d
- print "num=%1d" % num # output: num=14
% 3d print means 3-digit integer result when an integer number of bits is not enough 3, the left complement integer space, the print result is 14% 3d
- print "num=%3d" % num # output: num= 14
% -3d it means that the print result is three integers, when the number of integer digits is not enough 3, right side up in the integer space, so the print result is 14_% 3d
- print "num=%-3d" % num # output: num=14_
% 05d means that the printed result is an integer of 5, when the number of bits of the integer not 5, the left complement integer of 0, so the print result is 00014% 05d
- print "num=%05d" % num # output: num=00014
% .3d means 3 after the decimal point is printed result is an integer of 3,
When enough bits integer 3, the left complement integer 0, the print result is 014% .3d
- print "num=%.3d" % num # output: num=014
% .0003d 0003 and 3 after the decimal point, as expressed 3, meaning that a print result is an integer of 3,
When enough bits integer 3, the left complement integer of 0, so the print result or 014% .3d
- print "num=%.0003d" % num # output: num=014
% 5.3d is a comprehensive two kinds padded way when not enough 3-digit integers, first fill 0, is not enough five on the left, the left side and then fill in the blanks,
0 priority rules meant to supplement, the final length of the selected value larger, so the print result is still 014% 5.3d
- print "num=%5.3d" % num # output: num= 014
% 05.3d are filled comprehensive two kinds of ways, when the number of integer digits is not enough 3, 0, 5, or not enough to make up on the left,
Because it is 05, then the left 0s, the final length of the larger of the values selected, the print result or 00014% 05.3d
- print "num=%05.3d" % num # output: num=00014
- # Can also use% . D represents the value specified by the first two digits precision, two of * values in parentheses behind
As follows, but loses up this way 04 0 features that can only fill the space, only behind the decimal point 3 to fill 0
Print "NUM =% . D"% (04,3, NUM) # Output: NUM = 014
% float F- import math
% A.bf, a floating-point number represents a print length, b represents the decimal precision floating point behind
% F represent only the original value, the default is 5 digits after the decimal point
- print "PI=%f" % math.pi # output: PI=3.141593
Just when% 9f, represents a print length of 9 digits, also accounted for a decimal point, not enough space on the left side up
- print "PI=%9f" % math.pi # output: PI=_3.141593
Only when there is no behind the figures, it represents an integer of removing fractional output, a left side 03 up enough digits 0 3
- print "PI=%03.f" % math.pi # output: PI=003
% 6.3f after the decimal point indicates accurate to 3, the total length of 6 digits including a decimal point, not fill the spaces left
- print "PI=%6.3f" % math.pi # output: PI=_3.142
% -6.3f accurate after the decimal point indicates to 3, the total length of 6 digits including a decimal point, the right space if not
- print "PI=%-6.3f" % math.pi # output: PI=3.142_
- # Can also use% . F to represent the first two values specify the precision, the two * values in parentheses after the
As follows, but loses up this way 06 0 features that can only fill spaces
Print "the PI =% . F"% (06,3, Math.PI) # Output: = _3.142 the PI
Transfer https://blog.csdn.net/qq_37482544/article/details/63720726
Category: Python