String Formatting
Conversion | Meaning | Notes |
'd' | Signed integer decimal. |
|
'i' | Signed integer decimal. |
|
'o' | Signed octal value. | (1) |
'u' | Obsolete type – it is identical to 'd'. | (7) |
'x' | Signed hexadecimal (lowercase). | (2) |
'X' | Signed hexadecimal (uppercase). | (2) |
'e' | Floating point exponential format (lowercase). | (3) |
'E' | Floating point exponential format (uppercase). | (3) |
'f' | Floating point decimal format. | (3) |
'F' | Floating point decimal format. | (3) |
'g' | Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. | (4) |
'G' | Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. | (4) |
'c' | Single character (accepts integer or single character string). |
|
'r' | String (converts any Python object using repr()). | (5) |
's' | String (converts any Python object using str()). | (6) |
'%' | No argument is converted, results in a '%' character in the result. |
|
Specifier
%로 시작하는 스트링의 변환연산자이다.
다음의 순서에 따라 변환이 된다.
- The '%' character, which marks the start of the specifier.
- Mapping key (optional), consisting of a parenthesised sequence of characters (for example, (somename)).
- Conversion flags (optional), which affect the result of some conversion types.
- Minimum field width (optional). If specified as an '*' (asterisk), the actual width is read from the next element of the tuple in values, and the object to convert comes after the minimum field width and optional precision.
- Precision (optional), given as a '.' (dot) followed by the precision. If specified as '*' (an asterisk), the actual width is read from the next element of the tuple in values, and the value to convert comes after the precision.
- Length modifier (optional).
- Conversion type.
최 우측단에 dictionary 가 올 수 있는데 (or other mapping type), 그 때 그 스트링 안에 formats은 반드시 괄호안에 메핑키에 매핑이 되는 %가 있어야 한다.
>>>
>>> print '%(language)s has %(number)03d quote types.' % \
... {"language": "Python", "number": 2}
Python has 002 quote types.
In this case no * specifiers may occur in a format (since they require a sequential parameter list).
The conversion flag characters are:
Flag | Meaning |
'#' | The value conversion will use the "alternate form" (where defined below). |
'0' | The conversion will be zero padded for numeric values. |
'-' | The converted value is left adjusted (overrides the '0' conversion if both are given). |
' ' | (a space) A blank should be left before a positive number (or empty string) produced by a signed conversion. |
'+' | A sign character ('+' or '-') will precede the conversion (overrides a "space" flag). |
A length modifier (h, l, or L) may be present, but is ignored as it is not necessary for Python – so e.g. %ld is identical to %d.
f = 'pi with 3 decimal points %.3f'
from math import pi
print (f % pi)
f = 'pi with integer %i'
print (f % pi)
s = '%s + %s = %s' %(1,2,3)
print (s)
p = 'price of egg: %d' %42
print (p)
p = 'price of each egg: %d count %d total %d' %(2,3,2*3)
print (p)