3 ways Python string formatted output

% 1.  
    Print ( 'I'm% s, height% scm'% (name, height )) ** tuple is passed sequentially filled

    % s: placeholder STR ()  
    % D-: decimal integer
    % x: Hexadecimal
    % f: float
    specified length:
         % 5D right alignment, inadequate fill the spaces left
        % -5d - representing a left-aligned, is less than the right the default fill spaces
        % 05d right-aligned, is less than 0 up left
      
    floating point:
            % F 6 default output valid data will be rounded
            specified number of decimal places output% .2f --- reserved decimal place
            '% 4.8f' 4 is representative of the length of the float, including decimals, only when the length of the string is greater than 4 to function. spaces make up less than four, the space can make up 0 with 04.8%

      


2. format   
    characteristics: format string methods

    Filled pit sequence: {} placeholder
print ( '{} is the name, age {}' format ( 'Tom' , 20).)

Output:
The name is: Tom, age: 20
  subscript fill hole:
Print ( 'The name is: {1}, age is: {0}'. format ( 20, 'Tom'))

output:
The name is: Tom, age: 20
    variables filled pit: 'name is: {name}, age : '. the format (name =' {Age} Tom ', Age = 16)
Print (' name is: {name}, age: '. format (name =' {age} Tom ', age = 20))

output :
name is: Tom, age: 20

used in the variables
name = Ada
Age = 20
Print ( 'the name iS {name}, Age iS {Age}' the format (name = name, Age = Age).)
        {:. 5} length = 5 specifies the output
            string {:} 5 - left
            value {:} 5 - right-aligned
           using> <avoided string / numeric alignment of inconsistency
            > right-aligned
            <left

print ( 'name is: {0: * <11} \ n Age is: {1: *> 11} '. format ( 'Tom', 20))

Output:
The name is: Tom ********
age: 20 *********
      intermediate aligned ^ insufficient length indicated by *

print ( 'name is: {0: * ^ 11} \ n Age is: {1: * ^ 11} '. format ( 'Tom', 20))

Output:
The name is: **** Tom ****
age: 20 **** *****

3. Format f ''  

    After python3.6 version supports
    f 'name is: {name}, age: {age}'  

= name 'Tom'
Age = 20 is
Print (F 'name is: {name}, age: {age}')
 

Transfer: https: //blog.csdn.net/u012941152/article/details/81778006

Guess you like

Origin www.cnblogs.com/emma1325/p/12113095.html