% and format for string formatting

string formatting

There are two ways to format strings in Python: percent sign, format

1. Percent sign method

%[(name)][flags][width].[precision]typecode

    • (name) optional, used to select the specified key
    • flags is optional, the available values ​​are:
      • + Align right; add just before positive numbers, add a minus sign before negative numbers;
      • - Left justified; unsigned before positive numbers, minus sign before negative numbers;
      • Space is right-aligned; a space is added before positive numbers, and a minus sign is added before negative numbers;
      • 0 is right-aligned; unsigned before positive numbers, minus sign before negative numbers; fill blanks with 0
    • width is optional, occupying the width
    • .precision optional, the number of digits to retain after the decimal point
    • typecode required
      • s, get the return value of the __str__ method of the incoming object and format it to the specified location
      • r, get the return value of the __repr__ method of the incoming object and format it to the specified location
      • c, integer: convert the number to its unicode corresponding value, the decimal range is 0 <= i <= 1114111 (py27 only supports 0-255); character: add the character to the specified position
      • o, converts the integer to an octal representation and formats it to the specified location
      • x, converts the integer to a hexadecimal representation and formats it to the specified position
      • d, convert integers and floating-point numbers to decimal representation and format them to the specified position
      • e, convert integers and floating-point numbers to scientific notation and format them to the specified position (lowercase e)
      • E, convert integers and floating-point numbers to scientific notation and format them to the specified position (uppercase E)
      • f, convert integers and floating-point numbers into floating-point representations and format them to the specified position (6 decimal places are reserved by default)
      • F, as above
      • g, automatic adjustment converts integers and floating-point numbers into floating-point or scientific notation representation (more than 6 digits in scientific notation), and formats it to the specified position (if it is scientific notation, it is e;)
      • G, automatic adjustment converts integers and floating-point numbers to floating-point or scientific notation (more than 6 digits are represented by scientific notation), and formats it to the specified position (if it is scientific notation, it is E;)
      • %, when there is a formatting flag in the string, you need to use %% to represent a percent sign

Common formats

 

tpl = "i am %s" % "alex"
 
tpl = "i am %s age %d" % ("alex", 18)
 
tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
 
tpl = "percent %.2f" % 99.97623
 
tpl = "i am %(pp).2f" % {"pp": 123.425556, }
 
tpl = "i am %.2f %%" % {"pp": 123.425556, }

 

 

2.format format

Common formats:

tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
  
tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
  
tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
  
tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
  
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
  
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
  
tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
  
tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
  
tpl = "i am {:s}, age {:d}".format(*["seven", 18])
  
tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
  
tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
 
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
 
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
 
tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
 
tpl = " numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%} " .format(num=15)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325051855&siteId=291194637