Python3 Summary of print Function Usage
Category Programming Techniques
1. Outputting Strings and Numbers
>>>print("tutorialpro") # Output string
tutorialpro
>>> print(100) # Output number
100
>>> str = 'tutorialpro'
>>> print(str) # Output variable
tutorialpro
>>> 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. Formatting Integer Output
Supports argument formatting, similar to printf in C language
>>>str = "the length of (%s) is %d" %('tutorialpro',len('tutorialpro'))
>>> print(str)
the length of (tutorialpro) is 6
Python string formatting symbols:
| %c | Format character and its ASCII code | | %s | Format string | | %d | Format integer | | %u | Format unsigned integer | | %o | Format unsigned octal number | | %x | Format unsigned hexadecimal number | | %X | Format unsigned hexadecimal number (uppercase) | | %f | Format floating point number, specify precision after the decimal point | | %e | Format floating point number in scientific notation | | %E | Same as %e, format floating point number in scientific notation | | %g | Abbreviation for %f and %e | | %G | Abbreviation for %f and %E | | %p | Format the address of the variable in hexadecimal |
Formatting operator auxiliary instructions:
Symbol | Function |
---|---|
* | Define width or decimal precision |
- | Used for left alignment |
+ | Display a plus sign ( + ) in front of positive numbers |
<sp> | Display a space in front of positive numbers |
# | Display a zero ('0') in front of octal numbers, display '0x' or '0X' in front of hexadecimal numbers (depending on whether 'x' or 'X' is used) |
0 | Fill with '0' in front of the displayed number instead of the default space |
% | '%%' outputs a single '%' |
(var) | Map variable (dictionary argument) |
m.n. | m is the minimum total width displayed, n is the number of digits after the decimal point (if available) |
3. Formatting Output of Hexadecimal, Decimal, and 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. Formatting 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 the following tuple
pi = 3.142
>>> print('%010.3f' % pi) # Fill with zeros
000003.142
>>> print('%-10.3f' % pi) # Left align
3.142
>>> print('%+f' % pi) # Display sign
+3.141593
5. Automatic Line Wrapping, and by setting the separator parameter end, you can change its behavior.
>>>for i in range(0,6):
... print(i)
...
0
1
2
3
4
5
>>> for i in range(0,6):
... print(i, end=" ")
...
0 1 2 3 4 5
6. print without Line Break
In Python, print defaults to a line break:
>>>for i in range(0,3):
... print (i)
...
0
1
2
>>>
To avoid line breaks, you should write print(i, end = '')
```
for i in