Six print usage of Python Training

A, print () function Overview

print () method for printing out, the most common is a python function.
The syntax of this function is as follows:

print(*objects, sep=' ', end='\n', file=sys.stdout)

Parameters have the following specific meanings:
Objects - indicates the output of objects. A plurality of output objects, need, (comma).
sep - for a plurality of spaced objects.
end - the end of what used to be set. The default is a newline character \ n, we can be replaced with other characters.
file - the file object to write.
 

Print (. 1)   # numeric type can be directly output 
 
'' ' 
results are as follows 
. 1 
' '' 
 
Print ( " the Hello World " )   # string type can be directly output 
 
'' ' 
results are as follows: 
the Hello World 
' '' 
 
A =. 1 
B = " the Hello World " 
Print (A, B)   # may output a plurality of objects, objects separated by commas 
'' ' 
results are as follows: 
. 1 the Hello World 
' '' 
 
# if the direct output string, instead of the object represented by you can not use commas 
Print ( " Duan " " Yixuan ")
print("Duan " , " Yixuan " ) 
 
'' ' 
results are as follows: 
DuanYixuan 
Duan Yixuan 
understood, comma separator was not added, there is no gap between the string 
' '' 
 
Print ( " WWW " , " SNH48 " , " COM " , On Sep = " . " )   # set character spacing 
'' ' 
results are as follows: 
www.snh48.com 
' '' 
---------------------  
OF: TheGkeone 
source: CSDN 
original: HTTPS: //blog.csdn.net / sinat_28576553 / article / details / 81154912  
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!
View Code


Second, the output variables

No matter what type of data, including but not limited to: numeric, boolean, list of variables, variables ... dictionaries can be directly output.
% Characters: Conversion described Start tag breaks

 

1. Output variables such as strings and numbers

>>> Print ( " runoob " )   # output string 
runoob 
 >>> Print (100)             # output digital 
100 
>>> STR = ' runoob ' 
>>> Print (STR)             # output variable 
runoob
 >>> L = [ 1,2, ' A ' ]          # listing 
>>> Print (L)   
[ . 1, 2, ' A ' ]  
 >>> T = (1,2, ' A ')          # Tuple
>>> print(t)  
(1, 2, 'a')  
>>> d = {'a':1, 'b':2}    # 字典
>>> print(d)  
{'a': 1, 'b': 2}
View Code


2. Digital Output Formatting

>>>str = "the length of (%s) is %d" %('runoob',len('runoob'))
>>> print(str)
the length of (runoob) is 6
View Code

Format Character Description Format Character Description

% S using string str () to display a hexadecimal integer% x

% R string display (the repr ())% e index of the (substrate write e)

% E% c single character index (substrate write E)

% B binary integer% f,% F float

% G% d decimal integer exponent (e) or floating-point (the length of the display)

% I% G decimal integer exponent (E) or float (the length of the display)

% O octal integer character %%%
 

 3. Wrap

 In python, the output function is always default wrapping, or else if the wrap, the end parameter may be provided

for x in range(0, 5):
    print(x, end=' ')
#0 1 2 3 4 

If a print ( '\ n'), \ n newline is output, so that the direct print out, it corresponds to the output of the two newline
 

4. scientific notation

format(0.0015,'.2e')


The minimum field width and precision

Minimum field width: the converted string should have at least the value of a specified width. If * (asterisk), the width will be read from the value tuple.
Followed by precision value point (.): If the real output, the precision of decimal places is now shown after. If desired output string, then the number means the maximum field width. * If it is, then the accuracy will tuple read out.
Refer to implementation of the C language.
Note: The field width, also accounted for a decimal point.

= 3.141592653 the PI
 Print ( ' % 10.3f ' % the PI)   # field width 10, the accuracy of 3 
#      3.142 
 
# precision is 3, so only 142, the specified width is 10, so the need to add five spaces left to reach 10 width
View Code
= 3.1415926 the PI
 Print ( " the PI =%. * F " % (. 3 , the PI))
 # a * reading field width or precision from behind tuple, can be read out accuracy is 3 
# the PI = 3.142 
 
# No specified width, there is no need indent 
 
Print ( " the PI =% *. 3F " % (10, the PI))    # precision is 3, the total length of 10. the 
# the PI = 3.142 
 
# * in which the different positions, the read content different
View Code


6. Switch Flag

Switch Flag: - for left-aligned; + means to add value before the sign; "" (blank character) expressed reservations spaces (before a positive number); 0 indicates that the converted value if not enough digits 0 is filled with.
We can look at a specific example:

= 3.1415926 the PI 
Print ( '% - 10.3f' the PI%) # Left, or 10 characters, but the space on the right. 
# 3.142
= 3.1415926 the PI 
Print ( '% + f' the PI%) + # 3.141593 # sign display 
default precision f of type # 6 decimal places.
= 3.1415926 the PI 
Print ( '% 010.3f' the PI%) # field width 10, a precision of 3, the blank is filled with zeros at less than 
# 000003.142 conversion value 0 indicates 0 if not enough bits are filled with

  

 

Guess you like

Origin www.cnblogs.com/crucial/p/10943141.html