home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / m / more_strin < prev    next >
Text File  |  1996-11-14  |  3KB  |  55 lines

  1. <TITLE>More String Operations -- Python library reference</TITLE>
  2. Next: <A HREF="../m/mutable_sequence_types" TYPE="Next">Mutable Sequence Types</A>  
  3. Prev: <A HREF="../s/sequence_types" TYPE="Prev">Sequence Types</A>  
  4. Up: <A HREF="../s/sequence_types" TYPE="Up">Sequence Types</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H3>2.1.5.1. More String Operations</H3>
  7. String objects have one unique built-in operation: the <CODE>%</CODE>
  8. operator (modulo) with a string left argument interprets this string
  9. as a C sprintf format string to be applied to the right argument, and
  10. returns the string resulting from this formatting operation.
  11. <P>
  12. The right argument should be a tuple with one item for each argument
  13. required by the format string; if the string requires a single
  14. argument, the right argument may also be a single non-tuple object.<A NAME="footnoteref1" HREF="#footnotetext1">(1)</A>
  15. The following format characters are understood:
  16. %, c, s, i, d, u, o, x, X, e, E, f, g, G.
  17. Width and precision may be a * to specify that an integer argument
  18. specifies the actual width or precision.  The flag characters -, +,
  19. blank, # and 0 are understood.  The size specifiers h, l or L may be
  20. present but are ignored.  The <CODE>%s</CODE> conversion takes any Python
  21. object and converts it to a string using <CODE>str()</CODE> before
  22. formatting it.  The ANSI features <CODE>%p</CODE> and <CODE>%n</CODE>
  23. are not supported.  Since Python strings have an explicit length,
  24. <CODE>%s</CODE> conversions don't assume that <CODE>'\0'</CODE> is the end of
  25. the string.
  26. <P>
  27. For safety reasons, floating point precisions are clipped to 50;
  28. <CODE>%f</CODE> conversions for numbers whose absolute value is over 1e25
  29. are replaced by <CODE>%g</CODE> conversions.<A NAME="footnoteref2" HREF="#footnotetext2">(2)</A>
  30. All other errors raise exceptions.
  31. <P>
  32. If the right argument is a dictionary (or any kind of mapping), then
  33. the formats in the string must have a parenthesized key into that
  34. dictionary inserted immediately after the <CODE>%</CODE> character, and
  35. each format formats the corresponding entry from the mapping.  E.g.
  36. <UL COMPACT><CODE>    >>> count = 2<P>
  37.     >>> language = 'Python'<P>
  38.     >>> print '%(language)s has %(count)03d quote types.' % vars()<P>
  39.     Python has 002 quote types.<P>
  40.     >>> <P>
  41. </CODE></UL>
  42. In this case no * specifiers may occur in a format (since they
  43. require a sequential parameter list).
  44. <P>
  45. Additional string operations are defined in standard module
  46. <CODE>string</CODE> and in built-in module <CODE>regex</CODE>.
  47. <H2>---------- Footnotes ----------</H2>
  48. <A NAME="footnotetext1" HREF="#footnoteref1">(1)</A>
  49. A tuple object in this case should be a singleton.<P>
  50. <A NAME="footnotetext2" HREF="#footnoteref2">(2)</A>
  51. These numbers are fairly arbitrary.  They are intended to
  52. avoid printing endless strings of meaningless digits without hampering
  53. correct use and without having to know the exact precision of floating
  54. point values on a particular machine.<P>
  55.