The string formatting operator (%) is very similar to the string formatting of the printf () function in C, and even the symbols used are the same, all with percent sign (%), and all printf () format operations are supported. The syntax is as follows:
Format_String% String_to_convert
Format_String is a format-tagged string, in the form "%cdoe"; String_to_convert is a string to be formatted, and if it is more than two, it needs to be enclosed in parentheses.
Example of string format output:
CharA = 65charB = 66print ("ASCII code 65 for:%c"% CharA) print ("ASCII code 66 stands for:%c"% charb) Num1 = 0xef3num2 = 0xab03print (' converted to decimal: %u and%u '% (NUM1, Num2)) Num3 = 1500000print (' Convert to scientific notation:%e '% Num3)
Output:
ASCII code 65 means: A
ASCII code 66 means: B
Converted 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 handy.
Auxiliary symbols, as shown in the following table:
Examples of auxiliary symbols:
NUM1 = 108print ("% #X"% Num1) Num2 = 234.567890print ("%.2f"% Num2)
Output:
0x6c
234.57