One.
% form
A string that begins with a percent sign as a placeholder, substituting the syntax for a character after the string, and the column: "Today's weather is%s"% "Sunny". Note that there is a space before and after the%, and when there are multiple needs to be replaced, the strings used for substitution need to be enclosed in parentheses and separated by commas.
In addition, the placeholder has multiple forms,%d is for the int type, and%s is a string, but the integer and float types are also available, and%f is used for floating-point types, which can be controlled in the form of%.3f, where the number 3 represents the retention of three decimal places.
* If you want to output% in the print with the% form placeholder, you need to add a%, that is: percent to normal display%.
Two.
Format form
Unlike the% form, this form uses {} as a placeholder, in addition, the% form is a fixed syntax, and format is a function of the string in Python, so its usage is different, namely: "Today's weather is {}". Format ("Sunny"). When there are multiple substitutions, the strings used for substitution need to be enclosed in parentheses and separated by commas.
# formatted Output S12 = "I'm%s, I'm%d years old, I like%s"% (' Sylar ', 18, ' Jay ') # before the wording print (s12) S12 = "I'm {}, this year {} years old, I like {}". Format ("Jay Chou", 28, " Chow Yun-Fat ") # Format print by location (s12) S12 =" I call {0}, this year {2} years old, I like {1} ". Format (" Jay Chou "," Chow Yun-Fat ", 28) # Specify the location of print (s12) S12 =" I'm {name}, this year {age} " , I like {singer} ". Format (name=" Jay Chou ", singer=" Chow Yun-Fat ", age=28) # Specify Keywords
Formatted output of Python