Introduction of formatting character function
Application scenario: A placeholder is typically provided when print is available; Python provides two formatting strings: The first is the ancient way of using percent semicolons, the second is the enhanced formatting string, the Format function.
Second, the ancient percent of the way
1. Common usage
Common placeholders:%s-------------string placeholders
%d-------------digit placeholders
%f-------------floating-point digit placeholder
For example: Enter the user's information and print it out
Name= raw_input (' Input your Name: ') age= raw_input (' Input your Age: ') job = raw_input (' Input your job: ') msg= ' #定义 Variable msg, supplied with placeholder information of user%s:-------------------------------Name:%sage: %s# Special attention here, like the age of this through raw_ Input method-defined variables, python default is a string, so there is no%d, but with%s# if you want to use%d, then you can define the age of the age= int (raw_input (' Input your: ') Job: %s--------------End--------------'% (name,name,age,job) #把3个用户定义的变量依次赋给msg字符串中的各个占位符, implementing the replacement, Note 4 to correspond to print MSG results in order one by one: information of user mr.mangood:-------------------------------Name:Mr.MangoodAge: 27Job: Engineer--------------End--------------
2. More advanced usage
PLACEHOLDER structure: %[(name)][Flags][width]. [ Precision]typecode
which
(name) Optional, used to select the specified key
>>> info = ' Contact% ' (name) s of age is% (ages) d ' '%{' name ': ' Mr.mangood ', ' ' Age ': 23} # Here you can use the dictionary {' name ': ' Mr.mangood ', ' ' age ' : 23} to place placeholder values >>> Print info contact Mr.mangood Age is 23
[flags] optional, when the placeholder set the width of the time, such as set 20 characters, but when we pass the value of only 4 characters, then the concept of alignment appears, generally we will cooperate with [width] to
+ Right Align
-Align Left
Right-justified spaces
0 Align Right
[width] optional, define placeholder width
>>> info = ' Contact% ' (name) 20s age is% (ages) d ' ' '%{' name ': ' Mr.mangood ', ' age ': + 20 is width [width], does not write [flags] Default Right alignment >>> print info contact Mr.mangood Age is 23>>> info = ' Contact% ' (name) +20s is% (age) d ' '%{' name ': ' Mr.mangood ', ' age ': + +20 is right-aligned >>> print info contact Mr.mangood is 25>>> info = ' Contact% ' ( Name) The age of -20s is% (ages) d ' '%{' name ': ' Mr.mangood ', ' aging ': + # -20 Here is the left Alignment >>> print info contact Mr.mangood The age is 23
. Precision The number of digits left after the decimal point
>>> info = "Student% (name) s of the Age is% (ages) d average score is% (avgscore). 2f '%{' name ': ' Mr.mangood ', ' old ': ' Avgscore ' : 85.2479} #这里的. 2 represents%f this floating-point placeholder retains two decimal places >>> print Info The age of the student Mr.mangood is 25 with an average score of 85.25.
TypeCode must choose
C%c Converts the passed in integer number to its Unicode corresponding value
o convert integers to octal representations
X converts an integer to a hexadecimal representation
>>> s = "Test ASCII conversion%c test octal conversion%o Test 16 conversion%x"% (65,9,15) >>> print s test ASCII conversion a test octal conversion 11 Test 16 binary Conversion F
e convert integers and floating-point numbers into scientific notation (e in scientific notation is lowercase e)
e convert integers and floating-point numbers into scientific notation (e in the scientific notation is uppercase E)
>>> test = "Testing science and technology method%e test Science and Technology Law%e" "% (98888888,98888888) >>> Print Test Science and technology law 9.888889e+ 07 Test Science and Technology law 9.888889E+07
G automatically identifies whether the digital size needs to be converted into scientific notation (if more than 6 digits are represented by scientific notation), and the E in scientific notation is lowercase
G automatically identifies whether the digital size needs to be converted into scientific notation (if more than 6 digits are represented by scientific notation), and the G in scientific notation is capitalized
>>> test = "Testing science and technology method%e test Science and Technology Law%g" "% (123456,123456) >>> Print test science and technology 1.234560e+05 Test Science and technology law 123456
% when formatting flags appear in a string, you need to represent a%
>>> Zhenli = '%s is%d%% pure man '% (' mr.mangood ', ' + ') >>> print Zhenlimr.mangood is 100% pure man