Python print function usage, print format output

Source: Internet
Author: User
Tags print format



Original address: http://blog.csdn.net/zanfeng/article/details/52164124






Use print to output various types of

String
Integer
Floating point
Output and precision control
strHello = ‘Hello Python’
print strHello
#Output: Hello Python
#Directly out of the string


1. Format the output integer
Python print also supports parameter formatting, similar to C's printf,

strHello = "the length of (% s) is% d"% (‘Hello World’, len (‘Hello World’))
print strHello
#Output fruit: the length of (Hello World) is 11


2. Format the 16-bit integer
nHex = 0x20
#% x --- hex hex
#% d --- dec decimal
#% d --- oct octal
 
print "nHex =% x, nDec =% d, nOct =% o"% (nHex, nHex, nHex)
 
#Output results: nHex = 20, nDec = 32, nOct = 40
#Print the same number using the integer system


If you need to output binary, you can use the python function bin ()

Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> bin (789)
‘0b1100010101’
>>>


3. Format the output floating point number (float)
import math
#default
print "PI =% f"% math.pi
#width = 10, precise = 3, align = left
print "PI =% 10.3f"% math.pi
#width = 10, precise = 3, align = rigth
print "PI =% -10.3f"% math.pi
#Front Fill Character
print "PI =% 06d"% int (math.pi)
 
#Output result
#PI = 3.141593
#PI = 3.142
#PI = 3.142
#PI = 000003
#Floating point formatting, precision, degree and


4. Format the output string (string)
#precise = 3
print "% .3s"% ("jcodeer")
#precise = 4
print "%. * s"% (4, "jcodeer")
#width = 10, precise = 3
print "% 10.3s"% ("jcodeer")
#Output results:
#jco
#jcod
# jco
#Same as string, there are precision, degree sum.


5. Output list
l = [1,2,3,4, ‘jcodeer’]
print l
#Output result: [1, 2, 3, 4, ‘jcodeer’]
#Print directly on the list
‘‘ ‘6. dictionary (‘ dictionary ’‘ ‘
d = {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’
print d
#Output result: {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}
#Same as python also supports dictionary


6.python print wrap
print will automatically add a carriage return at the end of the line. If you don't need a carriage return, just add a comma "," at the end of the print statement to change its behavior.

for i in range (0,5):
    print i,


Or use the following function directly for output:

sys.stdout.write ("Output string")


7. Almighty% r
A colleague asked me what is the purpose of print "% r" in python and was asked.

I have used python for so many years and have never used print% r.

Checked online and found that% r is a universal format payment, it will print out the parameters given later with the type information.

python print% r case
formatter = "% r% r% r% r"
 
print formatter% (1, 2, 3, 4)
print formatter% ("one", "two", "three", "four")
print formatter% (True, False, False, True)
print formatter% (formatter, formatter, formatter, formatter)
print formatter% (
"I had this thing.",
"That you could type up right.",
 "But it did n’t sing.",
 "So I said goodnight."
 )


Output results:

$ python ex8.py
1 2 3 4
‘One’ ‘two’ ‘three’ ‘four’
True False False True
‘% R% r% r% r’ ‘% r% r% r% r’ ‘% r% r% r% r’ ‘% r% r% r% r’
‘I had this thing.’ ‘That you could type up right.’ “But it did n’t sing.” ‘So I said goodnight.’
$


Python print function usage, print formatted output


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.