Python 3 Print Function Usage Summary 1. Output string and number >>>print ("Runoob") # output string Runoob >>> print (100) # Output digit = >>> str = ' Runoob ' >> > Print (str) # output variable runoob >>> L = [N, ' a '] # list >>> print (L) [1, 2, ' a '] >>> t = ("a") # tuples >>> print (t) (1, 2, ' a ') >>> d = {' A ': 1, ' B ': 2} # Dictionary >>> print (d) {' A ': 1, ' B ': 2}2. Formatted output integers support parameter formatting, similar to the C language printf >>>str = "The length of (%s) is%d"% (' Runoob ', Len (' Runoob ')) >>> print ( STR) The length of (Runoob) is 6python string format symbol:
Symbol |
Describe |
%c |
Formatting characters and their ASCII code |
%s |
formatting strings |
%d |
formatting integers |
%u |
Formatting an unsigned integer |
%o |
Formatting an unsigned octal number |
%x |
formatting unsigned hexadecimal numbers |
%x |
Format unsigned hexadecimal number (uppercase) |
%f |
Format floating-point numbers to specify the precision after a decimal point |
%e |
Format floating-point numbers with scientific notation |
%E |
function with%e, format floating-point numbers with scientific notation |
%g |
Shorthand for%f and%e |
%G |
Shorthand for%f and%E |
%p |
Format the address of a variable with hexadecimal number |
Formatting operator Auxiliary directives:
Symbol |
Function |
* |
Define width or decimal precision |
- |
Used for left justification |
+ |
Show plus sign (+) in front of positive number |
<sp> |
Show spaces in front of positive numbers |
# |
Displays 0 (' 0 ') before the octal number, preceded by ' 0x ' or ' 0X ' in hexadecimal (depending on ' x ' or ' x ') |
0 |
The displayed number is preceded by ' 0 ' instead of the default space |
% |
' percent ' output a single '% ' |
(VAR) |
mapping variables (dictionary parameters) |
M.N. |
M is the minimum total width displayed, and n is the number of digits after the decimal point (if available) |
3. Formatted output 16 binary, decimal, octal integer #%x---hex hex #%d---Dec decimal #%o---oct octal >>>nhex = 0xFF >>> print ("Nhex =%x, Ndec =%d,noct =%o "% (nhex,nhex,nhex)) Nhex = Ff,ndec = 255,NOCT = 3774. Formatted output floating-point number (float) >>>pi = 3.141592653 >& gt;> print ('%10.3f '% pi) #字段宽10, accuracy 3 3.142 >>> print ("PI =%.*f"% (3,PI)) #用 * Read field width or precision from subsequent tuples pi = 3.142 > >> print ('%010.3f '% pi) #用0填充空白 000003.142 >>> print ('%-10.3f '% pi) #左对齐 3.142 >>> print ('%+f '% PI) #显示正负号 +3.1415935. Wrap 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. >>>for i in range (0,6): ... print (i,) ... 0 1 2 3 4 56. Print no wrap in Python print default is line wrap >>>for i in range (0,3): ... print (i) ... 0 1 2 >>> To change lines you should write print (i, end = ') >>>for i in Range (0,3): ... print (i, end = ") ... 012
Python 3 Print Function Usage Summary