--Old style formatting uses the format string% data. You can specify a width limit, a character limit, placement, and padding between the% and the type specifier.
#String
>>> "%s" % 42
`42`
#Decimal integer
>>> "%d" % 42
`42`
#Hexadecimal integer
>>> "%x" % 42
`2a`
#Octal integer
>>> "%o" % 42
`52`
#Decimal float
>>> "%f" % 7.03
`7.030000`
#Exponential form float
>>> "%e" % 7.03
`7.030000e+00`
#Depends on the size of the digit%g (Decimal float or exponential float)
>>> "%g" % 7.03
`7.03`
#Integer and literal%
>>> "%d%%" % 100
`100%`
>>> actor = "Richard Gere"
>>> cat="Chester"
>>> weight=28
#Insert string
>>> "My wife is favorite actor is %s" % actor
`My wife is favorite actor is Richard Gere`
#When inserting multiple character strings(cat,weight)Must be tupled like this.
>>> "Our cat %s weights %s pounds" % (cat,weight)
'Our cat Chester weights 28 pounds'
>>> n = 42
>>> f=7.03
>>> s="string cheese"
#Displayed with default width.
>>> "%s %f %s" %(n,f,s)
'42 7.030000 string cheese'
#A minimum width of 10 was set for each variable and right justified.
>>> "%10s %10f %10s" %(n,f,s)
' 42 7.030000 string cheese'
#Left-aligned using the same width.
>>> "%-10s %-10f %-10s" %(n,f,s)
'42 7.030000 string cheese'
#The width of the field is the same, and the upper limit of the number of characters is set to 4 and right-aligned. If you do this, part of the character string will be truncated, and the number after the decimal point will be 4 digits.
>>> "%10.4d %10.4f %10.4s" %(n,f,s)
' 0042 7.0300 stri'
#Limit the number of characters without specifying the lower limit of the field width.
>>> "%.4d %.4f %.4s" %(n,f,s)
'0042 7.0300 stri'
#Field width and number of characters*Argument by
>>> "%*.*d %*.*f %*.*s" %(10,4,n,10,4,f,10,4,s)
' 0042 7.0300 stri'
>>> "{} {} {}".format(n,f,s)
`42 7.03 string cheese`
#{}The number in indicates the index in format.
>>> "{2} {0} {1}".format(n,f,s)
`string cheese 42 7.03`
#The argument may be a dictionary or keyword argument. And it is also possible to put the key and name in the format specification.
>>> "{n} {f} {s}".format(n=42,f=7.03,s="string cheese")
`42 7.03 string cheese`
>>> d={"n":42,"f":7.03,"s":"string cheese"}
#{0}Is format()Refers to the dictionary called argument d in.
#{2}Is format()Refers to the character string other in the argument.
>>> "{0[n]} {0[f]} {0[s]} {1}".format(d,"other")
`42 7.03 string cheese other`
#%Instead of:Can be used.
>>> "{0:d} {1:f} {2:s}".format(n,f,s)
`42 7.030000 string cheese`
#Specified by keyword argument
>>> "{n:d} {f:f} {s:s}".format(n=42,f=7.03,s="string cheese")
`42 7.030000 string cheese`
#The default right justification with the lower limit of the field width set to 10.
>>> "{0:10d} {1:10f} {2:10s}".format(n,f,s)
` 42 7.030000 string cheese`
#Same right alignment as above, but>It is easier to understand by using.
>>> "{0:>10d} {1:>10f} {2:>10s}".format(n,f,s)
` 42 7.030000 string cheese`
#Left justified
>>> "{0:<10d} {1:<10f} {2:<10s}".format(n,f,s)
`42 7.030000 string cheese`
#Centered
>>> "{0:^10d} {1:^10f} {2:^10s}".format(n,f,s)
` 42 7.030000 string cheese`
#Unlike the old style, the precision specified after the decimal point specifies the number of digits after the decimal point for float and the upper limit for the number of characters for character strings, but it can no longer be used with integers.
>>> "{0:>10.4d} {1:>10.4f} {2:>10.4s}".format(n,f,s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Precision not allowed in integer format specifier
>>> "{0:>10d} {1:>10.4f} {2:>10.4s}".format(n,f,s)
` 42 7.0300 stri`
#:By specifying immediately after, and before specifying the alignment and width, the gaps in the output field can be filled with characters other than spaces.
>>> "{0:!^20s}".format("BIG SALE")
`!!!!!!BIG SALE!!!!!!`
The update frequency has become vacant recently. Posting was a little sloppy. .. ..
I'm studying, but I'm having a hard time with the database in Chapter 8. The story of the database is abstract and hard to come to my mind, but it seemed like I was getting an error even though I was proceeding according to the main story. Tomorrow I will take time to do Chapter 8 again. The continuation of Chapter 7 will be posted tomorrow.
"Introduction to Python3 by Bill Lubanovic (published by O'Reilly Japan)"
"Python Tutorial 3.8.1 Documentation 7. Input and Output" https://docs.python.org/ja/3/tutorial/inputoutput.html#old-string-formatting
Recommended Posts