This example describes the use of the print function in Python3.2. Share to everyone for your reference. The specific analysis is as follows:
1. Output string
?
1 2 3 |
>>> Strhello = "Hello World" >>> print (Strhello) Hello World |
2. Formatted output integer
Support parameter formatting, similar to the C language printf
?
1 2 3 |
>>> Strhello = "The length of (%s) is%d"% (' Hello World ', Len (' Hello World ')) >>> print (Strhello) The L Ength of (Hello World) is 11 |
3. Formatted output 16, decimal, octal integers
#%x---hex hex
#%d---Dec decimal
#%o---Oct octal
?
1 2 3 |
>>> Nhex = 0xFF >>> print ("Nhex =%x,ndec =%d,noct =%o"% (nhex,nhex,nhex)) Nhex = Ff,ndec = 255,noct = 377 |
4. Formatted output floating-point number (float)
?
1 2 3 4 5 6 7 8 9 |
Import Math >>> print (' pi=%f '%math.pi) pi=3.141593 >>> print ("PI =%10.3f"% math.pi) PI = 3.142 >&G t;> print ("PI =%-10.3f"% math.pi) pi = 3.142 >>> print ("PI =%06d"% int (math.pi)) pi = 000003 |
5. Formatted output floating-point number (float)
?
1 2 3 4 5 6 7 8 |
>>> precise = 3 >>> print ("%.3s"% ("python") pyt >>> precise = 4 >>> print ("%.*s" % (4, "Python")) Pyth >>> print ("%10.3s"% ("python") PYT |
6. Output lists (list)
Output list
?
1 2 3 |
>>> lst = [1,2,3,4, ' python '] >>> print (LST) [1, 2, 3, 4, ' Python '] |
Output dictionary
?
1 2 3 |
>>> d = {1: ' A ', 2: ' B ', 3: ' C ', 4: ' d '} >>> print (d) {1: ' A ', 2: ' B ', 3: ' C ', 4: ' d '} |
7. Automatic line Wrapping
Print automatically adds a carriage return at the end of the line, and if you do not need to enter, simply add a comma "," at the end of the print statement, and you can change its behavior.
?
1 2 3 4 5 6 7 8 9 |
>>> for I in Range (0,6): Print (I,) 0 1 2 3 4 5 |
or use the following function directly to output:
?
1 2 3 |
>>> Import sys >>> sys.stdout.write (' Hello World ') Hello World |
I hope this article will help you with your Python programming.