Use the print output for each type of
- String
- Integer
- Floating point number
- 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