home *** CD-ROM | disk | FTP | other *** search
- <TITLE>More String Operations -- Python library reference</TITLE>
- Next: <A HREF="../m/mutable_sequence_types" TYPE="Next">Mutable Sequence Types</A>
- Prev: <A HREF="../s/sequence_types" TYPE="Prev">Sequence Types</A>
- Up: <A HREF="../s/sequence_types" TYPE="Up">Sequence Types</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H3>2.1.5.1. More String Operations</H3>
- String objects have one unique built-in operation: the <CODE>%</CODE>
- operator (modulo) with a string left argument interprets this string
- as a C sprintf format string to be applied to the right argument, and
- returns the string resulting from this formatting operation.
- <P>
- The right argument should be a tuple with one item for each argument
- required by the format string; if the string requires a single
- argument, the right argument may also be a single non-tuple object.<A NAME="footnoteref1" HREF="#footnotetext1">(1)</A>
- The following format characters are understood:
- %, c, s, i, d, u, o, x, X, e, E, f, g, G.
- Width and precision may be a * to specify that an integer argument
- specifies the actual width or precision. The flag characters -, +,
- blank, # and 0 are understood. The size specifiers h, l or L may be
- present but are ignored. The <CODE>%s</CODE> conversion takes any Python
- object and converts it to a string using <CODE>str()</CODE> before
- formatting it. The ANSI features <CODE>%p</CODE> and <CODE>%n</CODE>
- are not supported. Since Python strings have an explicit length,
- <CODE>%s</CODE> conversions don't assume that <CODE>'\0'</CODE> is the end of
- the string.
- <P>
- For safety reasons, floating point precisions are clipped to 50;
- <CODE>%f</CODE> conversions for numbers whose absolute value is over 1e25
- are replaced by <CODE>%g</CODE> conversions.<A NAME="footnoteref2" HREF="#footnotetext2">(2)</A>
- All other errors raise exceptions.
- <P>
- If the right argument is a dictionary (or any kind of mapping), then
- the formats in the string must have a parenthesized key into that
- dictionary inserted immediately after the <CODE>%</CODE> character, and
- each format formats the corresponding entry from the mapping. E.g.
- <UL COMPACT><CODE> >>> count = 2<P>
- >>> language = 'Python'<P>
- >>> print '%(language)s has %(count)03d quote types.' % vars()<P>
- Python has 002 quote types.<P>
- >>> <P>
- </CODE></UL>
- In this case no * specifiers may occur in a format (since they
- require a sequential parameter list).
- <P>
- Additional string operations are defined in standard module
- <CODE>string</CODE> and in built-in module <CODE>regex</CODE>.
- <H2>---------- Footnotes ----------</H2>
- <A NAME="footnotetext1" HREF="#footnoteref1">(1)</A>
- A tuple object in this case should be a singleton.<P>
- <A NAME="footnotetext2" HREF="#footnoteref2">(2)</A>
- 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.<P>
-