''' Format String Place a string to be formatted to the left of the % operator, this string has one or more embedded conversion targets, all beginning with % (for example: %d) in % The right-hand side of the operator places one (or more, embedded in a tuple) objects that will be inserted on the left-hand side where one (or more) of the conversion targets you want Python to perform the format string on. Conversion target The general structure of : %[(name)][flags][width][.precision]typecode Any of the following operations can be performed between % and the character code: place a dictionary key; list left-aligned (-), Sign (+) and zero-padding (0) flags; give the overall length of the number and the number of digits after the decimal point, etc. ''' print ( 'that is %d %s bird' %( 1 , 'dead' )) s= 'Ni' print ( 'the knights who say %s' % s) print ( '%d %s %d you' %( 1 , 'spam' , 4 )) print ( '%s -- %s - - %s' %( 42 , 3.14159 ,[ 1 , 2 , 3 ])) #Integers, floats and lists are converted to strings x= 1234 y=- 1234 z= 1234567 print ( 'integers: .. .%d...' % x) # integers: ...1234... print ( 'integers: ...%6d...'% x) #Width 6 bits, default right-aligned integers: ... 1234... print ( 'integers: ...%-6d...' % x) #Width 6 bits, left-aligned integers: ... 1234 ... print ( 'integers: ...%+6d...' % x) #Display the sign integers: ... +1234... print ( 'integers: ...%+6d.. .' % y) #Display the sign integers: ... -1234... print ( 'integers: ...%+6d...' % y) #Display the sign integers: ... -1234 ... print ( 'integers: ...%06d...' % x) # zero-fill to make up 6-bit integers: ...001234... print ( 'integers: ...%06d...' % z) #0-fill 6-bit integers: ...1234567... x= 111.23456789 print ( '%e' % x) #1.112346e+02 print ( '%E' % x) #1.112346E+02 print ( '% f' % x) #111.234568 Default 6 decimal places print ( '%g' % x) #111.235 Default decimal point plus up to 6 digits print ( '...%-7.2f...' % x) #.. .111.23 ... retain two decimal places, display width 7, left justified print ( '...%07.2f...' % x) #...0111.23... retain two decimal places, display width 7, complement zero print ( '...%+7.1f...' % x) #... +111.2... Retain one decimal point, display width 7, display positive and negative signs #If you only know the size of the number at runtime, you can use a * in the format string to specify the calculated result. out width and percision, thus forcing their values to be taken from the next item in the output to the right of the % operator print ( '%f, %.2f, %.*f' % ( 1 / 3.0 , 1 / 3.0 , 4 , 1 / 3.0 )) #The 4 in the tuple is specified as precision #0.333333, 0.33, 0.3333 #Dictionary-based string formatting #String formatting also allows the conversion target on the left to refer to the key in the dictionary on the right to extract the corresponding Value print ( '%(n)d %(x)s' %{ "n" : 1 , "x" : "spam" }) #n and x refer to the keys in the dictionary on the right, and extract their corresponding values 1 spam reply= """ Greetings... hello %(name)s! Your age squared is %(age)s """ values={'name':'Bob', 'age':40} print(reply % values) #Use with the vars() function food= 'spam' age= 40 print ( vars ()) # The returned dictionary contains all the variables that existed when this function was called print ( '%(age)d %(food)s' % vars ()) #40 spam
python string formatting expression
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326001011&siteId=291194637
Recommended
Ranking