ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • String Formatting
    Python 2014. 5. 11. 00:21

    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

    % 시작하는 스트링의 변환연산자이다.

    다음의 순서에 따라 변환이 된다.

    1. The '%' character, which marks the start of the specifier.
    2. Mapping key (optional), consisting of a parenthesised sequence of characters (for example, (somename)).
    3. Conversion flags (optional), which affect the result of some conversion types.
    4. 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.
    5. 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.
    6. Length modifier (optional).
    7. 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 (hl, 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)

    'Python' 카테고리의 다른 글

    String short version & Template  (0) 2014.05.10
    Tuple  (0) 2014.05.09
    List & List member function  (0) 2014.05.09
    Slicing  (0) 2014.05.07
    연속된 문자열 처리  (0) 2014.05.07
Designed by Tistory.