Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:2.2.6.1 String MethodsUp:2.2.6 Sequence TypesNext:2.2.6.3 XRange Type

 
2.2.6.2 String Formatting Operations

 

String and Unicode objects have one unique built-in operation: the% operator (modulo). This is also known as the stringformatting orinterpolation operator. Givenformat %values (whereformat is a string orUnicode object),% conversion specifications informatare replaced with zero or more elements ofvalues. The effectis similar to the usingsprintf() in the C language. Ifformat is a Unicode object, or if any of the objects beingconverted using the%s conversion are Unicode objects, theresult will be a Unicode object as well.

Ifformat requires a single argument,values may be asingle non-tuple object.2.8 Otherwise,values must be a tuple withexactly the number of items specified by the format string, or asingle mapping object (for example, a dictionary).

A conversion specifier contains two or more characters and has thefollowing components, which must occur in this order:

  1. The "%" character, which marks the start of the specifier.
  2. Mapping key value (optional), consisting of an identifier in parentheses (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 invalues, 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 invalues, and the value to convert comes after the precision.
  6. Length modifier (optional).
  7. Conversion type.

If the right argument is a dictionary (or any kind of mapping), thenthe formats in the stringmust have a parenthesized key intothat dictionary inserted immediately after the "%"character, and each format formats the corresponding entry from themapping. For example:

>>> count = 2>>> language = 'Python'>>> print '%(language)s has %(count)03d quote types.' % vars()Python has 002 quote types.

In this case no* specifiers may occur in a format (since theyrequire a sequential parameter list).

The conversion flag characters are:

Flag Meaning 
#The value conversion will use the ``alternate form'' (where defined below).
0The 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).

The length modifier may beh,l, andL may bepresent, but are ignored as they are not necessary for Python.

The conversion types are:

Conversion Meaning Notes 
dSigned integer decimal. 
iSigned integer decimal. 
oUnsigned octal.(1)
uUnsigned decimal. 
xUnsigned hexidecimal (lowercase).(2)
XUnsigned hexidecimal (uppercase).(2)
eFloating point exponential format (lowercase). 
EFloating point exponential format (uppercase). 
fFloating point decimal format. 
FFloating point decimal format. 
gSame as "e" if exponent is greater than -4 or less than precision, "f" otherwise. 
GSame as "E" if exponent is greater than -4 or less than precision, "F" otherwise. 
cSingle character (accepts integer or single character string). 
rString (converts any python object usingrepr()).(3)
sString (converts any python object usingstr()).(4)
%No argument is converted, results in a "%" character in the result. 

Notes:

(1)
The alternate form causes a leading zero ("0") to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.
(2)
The alternate form causes a leading'0x' or'0X' (depending on whether the "x" or "X" format was used) to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.
(3)
The%r conversion was added in Python 2.0.
(4)
If the object or format provided is aunicode string, the resulting string will also beunicode.

Since Python strings have an explicit length,%s conversionsdo not assume that'\0' is the end of the string.

For safety reasons, floating point precisions are clipped to 50;%f conversions for numbers whose absolute value is over 1e25are replaced by%g conversions.2.9 All other errors raise exceptions.

Additional string operations are defined in standard modulesstring andre. 



Footnotes

... object.2.8
A tuple object in this case should be a singleton.
... conversions.2.9
These numbers are fairly arbitrary. They are intended to avoid printing endless strings of meaningless digits without hampering correct use and without having to know the exact precision of floating point values on a particular machine.


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:2.2.6.1 String MethodsUp:2.2.6 Sequence TypesNext:2.2.6.3 XRange Type
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp