Summary of Python3 print function usage
Python 3 makes the print function more explicit compared to Python 2.
1. Outputting Strings and Numbers
print("runoob") # Outputs string runoob
print(100) # Outputs number 100
str = 'runoob'
print(str) # Outputs variable runoob
L = [1,2,'a'] # List
print(L) [1, 2, 'a']
t = (1,2,'a') # Tuple
print(t) (1, 2, 'a')
d = {'a':1, 'b':2} # Dictionary
print(d) {'a': 1, 'b': 2}
2. Formatted Integer Output
Supports parameter formatting, similar to C language’s printf.
str = "the length of (%s) is %d" %('runoob',len('runoob'))
print(str) # the length of (runoob) is 6
Python String Formatting Symbols:
| ** Symbol** | Description |
|---|---|
| %c | Formats character and its ASCII code |
| %s | Formats string |
| %d | Formats signed decimal integer |
| %u | Formats unsigned decimal integer |
| %o | Formats unsigned octal number |
| %x | Formats unsigned hexadecimal number (lowercase) |
| %X | Formats unsigned hexadecimal number (uppercase) |
| %f | Formats floating-point number, precision can be specified after decimal point |
| %e | Formats floating-point number in scientific notation (lowercase ’e') |
| %E | Same as %e, formats floating-point number in scientific notation (uppercase ‘E’) |
| %g | Shorthand for %f and %e |
| %G | Shorthand for %f and %E |
| %p | Formats variable’s address in hexadecimal |
Formatting Operator Auxiliary Directives:
| ** Symbol** | Function |
|---|---|
| * | Defines width or decimal precision |
| - | Used for left alignment |
| + | Displays a plus sign (+) before positive numbers |
| (space) | Displays a space before positive numbers |
| # | Displays ‘0’ before octal numbers, ‘0x’ or ‘0X’ before hexadecimal numbers (depending on ‘x’ or ‘X’) |
| 0 | Pads the number with ‘0’ instead of default spaces |
| % | ‘%%’ outputs a single ‘%’ |
| (var) | Maps variable (dictionary parameter) |
| m.n. | m is the minimum total width displayed, n is the number of digits after the decimal point (if available) |
3. Formatted Output of Hexadecimal, Decimal, Octal Integers
#%x — hex hexadecimal
#%d — dec decimal
#%o — oct octal
nHex = 0xFF
print("nHex = %x,nDec = %d,nOct = %o" %(nHex,nHex,nHex)) # nHex = ff,nDec = 255,nOct = 377
4. Formatted Output of Floating-Point Numbers (float)
pi = 3.141592653
print('%10.3f' % pi) # field width 10, precision 3 -> 3.142
print("pi = %.*f" % (3,pi)) # use * to read field width or precision from trailing tuple -> pi = 3.142
print('%010.3f' % pi) # pad with 0 -> 000003.142 >>> print('%-10.3f' % pi) # left-align -> 3.142
print('%+f' % pi) # display sign -> +3.141593
5. Automatic Line Break
print automatically adds a newline character at the end of the line. If a newline is not desired, simply add a comma , at the end of the print statement to change its behavior.
for i in range(0,6):
... print (i,) ...
0 1 2 3 4 5
6. Print Without Newline
In Python, print defaults to adding a newline:
>>>for i in range(0,3):
... print (i) ...
0
1
2
# To prevent a newline, you should write **print(i, end = " ")**
>>>for i in range(0,3):
... print(i, end = " ") ...
012
- 原文作者:春江暮客
- 原文链接:https://www.bobobk.com/en/323.html
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。