Table 1.1. String Formatting code
| format |
description |
| %% |
percent sign |
| % c |
character and its ASCII code |
| %s |
string |
| %d |
signed integer (decimal) |
| %u |
unsigned integer (decimal) |
| %o |
unsigned integer (octal) |
| %x |
None Symbol integer (hex) |
| % X |
unsigned integer (16 uppercase characters) |
| % e |
Floating-point number (scientific notation) |
| % E |
floating-point numbers (scientific notation, E instead of e) |
| %f |
floating-point numbers (with decimal point symbols) |
| %g |
floating-point numbers (%e or%f based on value) |
| % g |
floating-point numbers (similar to%g) |
| %p |
pointer (memory address to print values in hexadecimal) |
| %n |
stores the number of output characters into the next variable in the parameter list |
Python print formatted output.
1. Print a string
Print ("His name is%s"% ("Aviad"))
Effect:
2. Printing integers
Print ("He is%d years old"% (25))
Effect:
3. Print floating-point numbers
Print ("His height is%f m"% (1.83))
Effect:
4. Print floating-point numbers (Specify the number of decimal places reserved)
Print ("His height is%.2f m"% (1.83))
Effect:
5. Specify the placeholder width
Print ("name:%10s age:%8d height:%8.2f"% ("Aviad", 25,1.83))
Effect:
6. Specify the placeholder width (left-justified)
Print ("name:%-10s age:%-8d height:%-8.2f"% ("Aviad", 25,1.83))
Effect:
7. Specify placeholders (only 0 placeholders?). )
Print ("name:%-10s age:%08d height:%08.2f"% ("Aviad", 25,1.83))
Effect:
8. Scientific counting method
Format (0.0015, '. 2e ')
Effect:
Python formatted output