Python-% formatted output (10)

Formatting Output variables: input function to output information to the console with the output variables and literals.
% Formatting operator, and used in conjunction with different characters, strings% S,% d decimal integer, floating-point F%
** Method: ** create a string, where necessary output variables accounted for by the number of cells%, in after the entire string% plus a variable name

1. Float formatted output

= name 'xiaoming'
Print ( 'My name is:% s, please take care'% name)

2. formatted output integer

% 0xd x-bit dissatisfied with leading zeros, as is more than x-bit display

= 112 student_no
Print ( 'my student number is:% 07d'% student_no)

3. The floating point formatted output

% .Xf x bits behind the decimal point

= 8.5. price
weight = 7.5
Money = weight. price *
Print ( 'Apple Price:.% 2f yuan / kg, bought a% f.3 pounds, to a total of:.% 4f RMB'% (price, weoight, money ))

4. Percent formatted output%

= 0.25 Scale
Print ( 'data ratio is .2f %%%'% (Scale 100)) # (Scale 100) without parentheses cause string concatenation 100

Guess you like

Origin blog.csdn.net/sinat_40624829/article/details/89554995