Python Print format output

Source: Internet
Author: User
Tags print format

Use the print output for each type of

    1. String
    2. Integer
    3. Floating point number
    4. Out-of-degree and precision control
Print StrhelloPython#直接出字符串

1. Format the output integer

Python print also supports parameter formatting, like the C-word printf,

%(' Hello World ',len(' Hello World '))print Strhello#输出果: The length of (Hello World )         is one

2. Format output 16-made integers
Nhex = 0x20#%x---hex hex #%d---Dec decimal #%d---oct octal   %(Nhex,nhex,nhex) # Output result: Nhex = 20,ndec = 32,noct =#使用整数的各个制打印同一个数
3. Formatted output floating-point number (float)
ImportMath#defaultPrint"PI =%f"%Math. pi#width = 10,precise = 3,align = leftprint "PI =%10.3f" % Math.  Pi#width = 10,precise = 3,align = rigthprint "PI =%-10.3f" % Math.  Pi#前面填充字符print "PI =%06d" % int(math.  Pi) #输出结果#PI = 3.141593#PI = 3.142#PI = 3.142#PI = 000003#浮点数的格式化, accuracy, degree, and  /c26> 

4. Format the output string (string)
#precise = 3("Jcodeer")#precise = 4(4,"Jcodeer")#width = 10, precise = 3("Jcodeer")#输出结果:#jco#jcod# jco#同于字符串也存在精度, degrees, and. 

5. Output lists (list)
[1,2,3,4,' Jcodeer ']print L#输出结果: [1, 2, 3, 4, ' Jcodeer ']#于list直接打印即可"  ' 6. Out dictionary (dictionary) '{1:' A ',2:' B ',3:' C ',4:' d '}print D  #输出结果: {1: ' A ', 2: ' B ', 3: ' C ', 4: ' D '}#同Python is also supported by dictionary       

6.python print wrap line

Print automatically adds a carriage return at the end of the line, and if you don't need a carriage return, simply add a comma "," at the end of the print statement to change its behavior.

Range(0,5print I,   

Or simply use the following function to output:

Sys. stdout. Write("output string")    

7. The Almighty%r

A colleague asked me what the use of the print "%r" in Python was and was asked to fall.

With all these years of Python, I didn't use print%r.

Online check, found that%r is a universal format to pay, it will be given the parameters are printed out, with type information.

python print%r case
Formatter ="%r%r%r%r"PrintFormatter%(1,2,3,4)PrintFormatter%("One","The Other", "three",  "four" ) print  Formatter %  (true, false, false, true) print  Formatter %  (formatter, formatter, formatter, formatter) print  Formatter %  ( "I had this thing.",  "so you Could type up right. ", " but it didn ' t sing. ", ) 

Output Result:

Python ex8.py1 2 3 4 ' One ', ' 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's could type up right. ' "But it didn ' t sing." ' So I said Goodnight. ' $

Python Print format 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.