Python inside the print usage%, "% s and% d" representative of the meaning of

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

  1. string="hello"
  2. When the printing result is hello% s

  3. print "string=%s" % string # output: string=hello
  4. % 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

  5. print "string=%2s" % string # output: string=hello
  6. % 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,

  7. So the print result is hello% 7s

  8. print "string=%7s" % string # output: string= hello
  9. % -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,

  10. So the print result is hello% -7s

  11. print "string=%-7s!" % string # output: string=hello !
  12. % .2s means intercepts the first two characters of the string, the print result is he% .2s

  13. print "string=%.2s" % string # output: string=he
  14. % .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,

  15. So the print result is hello% .7s

  16. print "string=%.7s" % string # output: string=hello
  17. % A.bs This format is an integrated two formats above, according to the first taken after the decimal point character string number B,

  18. When taken string length is less than a, which also need to fill a space left

  19. print "string=%7.2s" % string # output: string= he
  20. print "string=%2.7s" % string # output: string=hello
  21. print "string=%10.7s" % string # output: string= hello
  22. # Can also use% . S to represent precision, the two values * parentheses after the first two bits of the specified value
  23. print "string=%.s" % (7,2,string) # output: string= he
    %d 整型
  24. a = 14
  25. % D 14 is the print result

  26. print "num=%d" % num # output: num=14
  27. % 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

  28. print "num=%1d" % num # output: num=14
  29. % 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

  30. print "num=%3d" % num # output: num= 14
  31. % -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

  32. print "num=%-3d" % num # output: num=14_
  33. % 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

  34. print "num=%05d" % num # output: num=00014
  35. % .3d means 3 after the decimal point is printed result is an integer of 3,

  36. When enough bits integer 3, the left complement integer 0, the print result is 014% .3d

  37. print "num=%.3d" % num # output: num=014
  38. % .0003d 0003 and 3 after the decimal point, as expressed 3, meaning that a print result is an integer of 3,

  39. When enough bits integer 3, the left complement integer of 0, so the print result or 014% .3d

  40. print "num=%.0003d" % num # output: num=014
  41. % 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,

  42. 0 priority rules meant to supplement, the final length of the selected value larger, so the print result is still 014% 5.3d

  43. print "num=%5.3d" % num # output: num= 014
  44. % 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,

  45. 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

  46. print "num=%05.3d" % num # output: num=00014
  47. # Can also use% . D represents the value specified by the first two digits precision, two of * values in parentheses behind
  48. 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

  49. Print "NUM =% . D"% (04,3, NUM) # Output: NUM = 014
    % float F

  50. import math
  51. % A.bf, a floating-point number represents a print length, b represents the decimal precision floating point behind

  52. % F represent only the original value, the default is 5 digits after the decimal point

  53. print "PI=%f" % math.pi # output: PI=3.141593
  54. Just when% 9f, represents a print length of 9 digits, also accounted for a decimal point, not enough space on the left side up

  55. print "PI=%9f" % math.pi # output: PI=_3.141593
  56. 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

  57. print "PI=%03.f" % math.pi # output: PI=003
  58. % 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

  59. print "PI=%6.3f" % math.pi # output: PI=_3.142
  60. % -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

  61. print "PI=%-6.3f" % math.pi # output: PI=3.142_
  62. # Can also use% . F to represent the first two values specify the precision, the two * values in parentheses after the
  63. As follows, but loses up this way 06 0 features that can only fill spaces

  64. 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

Guess you like

Origin www.cnblogs.com/abdm-989/p/11360426.html