Python string formatted output

 


 This link: https://www.cnblogs.com/zyuanlbj/p/11910913.html


 % S placeholder

= name ' boats ' 
Print ( ' name is: S% ' % name)

format () function

Format:. "{} {}" Format (value, value)

Example:

= name ' Tom ' 
Age =. 7 
Hobby = ' play the slides! ' 
Money = 8.5 
the Message = ' {} {} years old this year, like most {}, there pocket money: {} ' .format (name, Age, Hobby, Money)
 Print (the Message)   # Tom is 7 years old, most likes to play slippery slides! There pocket money: $ 850

Python conversion specifier

Conversion specifier Explanation
%d,%i Convert integer to signed decimal notation
%O Integer converted to signed octal form
%x,%X Signed integer is converted to hexadecimal form
%e Converted to a floating (e lowercase) represented by the scientific notation
%E Converted to a floating (E uppercase) represented by the scientific notation
%f,%F Converted to decimal floating-point number
%g Or intelligent selection using% f% e format
%G Smart or choose to use% F% E format
%c Formatting characters and their ASCII code
%r Using the repr () converts a string variable or expression
%s Use str () converts a string variable or expression

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Example:

# % D digit digital
age = 18
Print ( ' age: D% ' % Age)   

Age = 18.5    # int (18.5) ---> 18 is rounded 
Print ( ' age: D% ' % Age)  

year = 2019
 Print ( ' this year is:% 02d ' % year)   # still 2019 but can be set-digit% f

# Behind% f float is the number of decimal places and rounded
salary=8899.32895
Print ( ' My salary is:.% 2F ' % salary)

Guess you like

Origin www.cnblogs.com/zyuanlbj/p/11910913.html