In simple terms, the Python string formatting operator (%) is very similar to the string formatting of the printf () function in C language, and even the symbols used are the same. the percent sign (%) is used ), all printf () formatting operations are supported. Syntax:
Format_string % string_to_convert
Format_string is a string in the format of "% cdoe"; string_to_convert is the string to be formatted. if there are more than two strings, they must be enclosed in parentheses.
Example of string formatting output:
CharA = 65 charB = 66 print ("ASCII code 65 stands for: % c" % charA) print ("ASCII code 66 stands for: % c" % charB) num1 = 0xEF3Num2 = 0xAB03print ('convert to decimal: % u and % u' % (Num1, Num2) Num3 = 1500000 print ('convert to scientific notation: % E' % Num3)
Output:
ASCII code 65:
ASCII code 66: B
Convert to decimal: 3827 and 43779, respectively.
Convert to scientific notation: 1.200000e + 06
Formatting characters can also be used with auxiliary symbols, which is very convenient.
Auxiliary symbols, as shown in the following table:
Auxiliary symbol example:
Num1 = 108 print ("% # X" % Num1) Num2 = 234.567890 print ("%. 2f" % Num2)
Output:
0X6C
234.57