home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / s / string < prev    next >
Encoding:
Text File  |  1996-11-14  |  10.6 KB  |  202 lines

  1. <TITLE>string -- Python library reference</TITLE>
  2. Next: <A HREF="../r/regex" TYPE="Next">regex</A>  
  3. Prev: <A HREF="../s/string_services" TYPE="Prev">String Services</A>  
  4. Up: <A HREF="../s/string_services" TYPE="Up">String Services</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H1>4.1. Standard Module <CODE>string</CODE></H1>
  7. This module defines some constants useful for checking character
  8. classes and some useful string functions.  See the modules
  9. <CODE>regex</CODE> and <CODE>regsub</CODE> for string functions based on regular
  10. expressions.
  11. <P>
  12. The constants defined in this module are are:
  13. <P>
  14. <DL><DT><B>digits</B> -- data of module string<DD>
  15. The string <CODE>'0123456789'</CODE>.
  16. </DL>
  17. <DL><DT><B>hexdigits</B> -- data of module string<DD>
  18. The string <CODE>'0123456789abcdefABCDEF'</CODE>.
  19. </DL>
  20. <DL><DT><B>letters</B> -- data of module string<DD>
  21. The concatenation of the strings <CODE>lowercase</CODE> and
  22. <CODE>uppercase</CODE> described below.
  23. </DL>
  24. <DL><DT><B>lowercase</B> -- data of module string<DD>
  25. A string containing all the characters that are considered lowercase
  26. letters.  On most systems this is the string
  27. <CODE>'abcdefghijklmnopqrstuvwxyz'</CODE>.  Do not change its definition ---
  28. the effect on the routines <CODE>upper</CODE> and <CODE>swapcase</CODE> is
  29. undefined.
  30. </DL>
  31. <DL><DT><B>octdigits</B> -- data of module string<DD>
  32. The string <CODE>'01234567'</CODE>.
  33. </DL>
  34. <DL><DT><B>uppercase</B> -- data of module string<DD>
  35. A string containing all the characters that are considered uppercase
  36. letters.  On most systems this is the string
  37. <CODE>'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</CODE>.  Do not change its definition ---
  38. the effect on the routines <CODE>lower</CODE> and <CODE>swapcase</CODE> is
  39. undefined.
  40. </DL>
  41. <DL><DT><B>whitespace</B> -- data of module string<DD>
  42. A string containing all characters that are considered whitespace.
  43. On most systems this includes the characters space, tab, linefeed,
  44. return, formfeed, and vertical tab.  Do not change its definition ---
  45. the effect on the routines <CODE>strip</CODE> and <CODE>split</CODE> is
  46. undefined.
  47. </DL>
  48. The functions defined in this module are:
  49. <P>
  50. <DL><DT><B>atof</B> (<VAR>s</VAR>) -- function of module string<DD>
  51. Convert a string to a floating point number.  The string must have
  52. the standard syntax for a floating point literal in Python, optionally
  53. preceded by a sign (`<SAMP>+</SAMP>' or `<SAMP>-</SAMP>').
  54. </DL>
  55. <DL><DT><B>atoi</B> (<VAR>s</VAR>[, <VAR>base</VAR>]) -- function of module string<DD>
  56. Convert string <VAR>s</VAR> to an integer in the given <VAR>base</VAR>.  The
  57. string must consist of one or more digits, optionally preceded by a
  58. sign (`<SAMP>+</SAMP>' or `<SAMP>-</SAMP>').  The <VAR>base</VAR> defaults to 10.  If it is
  59. 0, a default base is chosen depending on the leading characters of the
  60. string (after stripping the sign): `<SAMP>0x</SAMP>' or `<SAMP>0X</SAMP>' means 16,
  61. `<SAMP>0</SAMP>' means 8, anything else means 10.  If <VAR>base</VAR> is 16, a
  62. leading `<SAMP>0x</SAMP>' or `<SAMP>0X</SAMP>' is always accepted.  (Note: for a more
  63. flexible interpretation of numeric literals, use the built-in function
  64. <CODE>eval()</CODE>.)
  65. </DL>
  66. <DL><DT><B>atol</B> (<VAR>s</VAR>[, <VAR>base</VAR>]) -- function of module string<DD>
  67. Convert string <VAR>s</VAR> to a long integer in the given <VAR>base</VAR>.  The
  68. string must consist of one or more digits, optionally preceded by a
  69. sign (`<SAMP>+</SAMP>' or `<SAMP>-</SAMP>').  The <VAR>base</VAR> argument has the same
  70. meaning as for <CODE>atoi()</CODE>.  A trailing `<SAMP>l</SAMP>' or `<SAMP>L</SAMP>' is not
  71. allowed, except if the base is 0.
  72. </DL>
  73. <DL><DT><B>capitalize</B> (<VAR>word</VAR>) -- function of module string<DD>
  74. Capitalize the first character of the argument.
  75. </DL>
  76. <DL><DT><B>capwords</B> (<VAR>s</VAR>) -- function of module string<DD>
  77. Split the argument into words using <CODE>split</CODE>, capitalize each word
  78. using <CODE>capitalize</CODE>, and join the capitalized words using
  79. <CODE>join</CODE>.  Note that this replaces runs of whitespace characters by
  80. a single space.  (See also <CODE>regsub.capwords()</CODE> for a version
  81. that doesn't change the delimiters, and lets you specify a word
  82. separator.)
  83. </DL>
  84. <DL><DT><B>expandtabs</B> (<VAR>s</VAR>, <VAR>tabsize</VAR>) -- function of module string<DD>
  85. Expand tabs in a string, i.e. replace them by one or more spaces,
  86. depending on the current column and the given tab size.  The column
  87. number is reset to zero after each newline occurring in the string.
  88. This doesn't understand other non-printing characters or escape
  89. sequences.
  90. </DL>
  91. <DL><DT><B>find</B> (<VAR>s</VAR>, <VAR>sub</VAR>[, <VAR>start</VAR>]) -- function of module string<DD>
  92. Return the lowest index in <VAR>s</VAR> not smaller than <VAR>start</VAR> where the
  93. substring <VAR>sub</VAR> is found.  Return <CODE>-1</CODE> when <VAR>sub</VAR>
  94. does not occur as a substring of <VAR>s</VAR> with index at least <VAR>start</VAR>.
  95. If <VAR>start</VAR> is omitted, it defaults to <CODE>0</CODE>.  If <VAR>start</VAR> is
  96. negative, <CODE>len(<VAR>s</VAR>)</CODE> is added.
  97. </DL>
  98. <DL><DT><B>rfind</B> (<VAR>s</VAR>, <VAR>sub</VAR>[, <VAR>start</VAR>]) -- function of module string<DD>
  99. Like <CODE>find</CODE> but find the highest index.
  100. </DL>
  101. <DL><DT><B>index</B> (<VAR>s</VAR>, <VAR>sub</VAR>[, <VAR>start</VAR>]) -- function of module string<DD>
  102. Like <CODE>find</CODE> but raise <CODE>ValueError</CODE> when the substring is
  103. not found.
  104. </DL>
  105. <DL><DT><B>rindex</B> (<VAR>s</VAR>, <VAR>sub</VAR>[, <VAR>start</VAR>]) -- function of module string<DD>
  106. Like <CODE>rfind</CODE> but raise <CODE>ValueError</CODE> when the substring is
  107. not found.
  108. </DL>
  109. <DL><DT><B>count</B> (<VAR>s</VAR>, <VAR>sub</VAR>[, <VAR>start</VAR>]) -- function of module string<DD>
  110. Return the number of (non-overlapping) occurrences of substring
  111. <VAR>sub</VAR> in string <VAR>s</VAR> with index at least <VAR>start</VAR>.
  112. If <VAR>start</VAR> is omitted, it defaults to <CODE>0</CODE>.  If <VAR>start</VAR> is
  113. negative, <CODE>len(<VAR>s</VAR>)</CODE> is added.
  114. </DL>
  115. <DL><DT><B>lower</B> (<VAR>s</VAR>) -- function of module string<DD>
  116. Convert letters to lower case.
  117. </DL>
  118. <DL><DT><B>maketrans</B> (<VAR>from</VAR>, <VAR>to</VAR>) -- function of module string<DD>
  119. Return a translation table suitable for passing to <CODE>string.translate</CODE>
  120. or <CODE>regex.compile</CODE>, that will map each character in <VAR>from</VAR> 
  121. into the character at the same position in <VAR>to</VAR>; <VAR>from</VAR> and
  122. <VAR>to</VAR> must have the same length. 
  123. </DL>
  124. <DL><DT><B>split</B> (<VAR>s</VAR>[, <VAR>sep</VAR>[, <VAR>maxsplit</VAR>]]) -- function of module string<DD>
  125. Return a list of the words of the string <VAR>s</VAR>.  If the optional
  126. second argument <VAR>sep</VAR> is absent or <CODE>None</CODE>, the words are
  127. separated by arbitrary strings of whitespace characters (space, tab,
  128. newline, return, formfeed).  If the second argument <VAR>sep</VAR> is
  129. present and not <CODE>None</CODE>, it specifies a string to be used as the
  130. word separator.  The returned list will then have one more items than
  131. the number of non-overlapping occurrences of the separator in the
  132. string.  The optional third argument <VAR>maxsplit</VAR> defaults to 0.  If
  133. it is nonzero, at most <VAR>maxsplit</VAR> number of splits occur, and the
  134. remainder of the string is returned as the final element of the list
  135. (thus, the list will have at most <CODE><VAR>maxsplit</VAR>+1</CODE> elements).
  136. (See also <CODE>regsub.split()</CODE> for a version that allows specifying a
  137. regular expression as the separator.)
  138. </DL>
  139. <DL><DT><B>splitfields</B> (<VAR>s</VAR>[, <VAR>sep</VAR>[, <VAR>maxsplit</VAR>]]) -- function of module string<DD>
  140. This function behaves identical to <CODE>split</CODE>.  (In the past,
  141. <CODE>split</CODE> was only used with one argument, while <CODE>splitfields</CODE>
  142. was only used with two arguments.)
  143. </DL>
  144. <DL><DT><B>join</B> (<VAR>words</VAR>[, <VAR>sep</VAR>]) -- function of module string<DD>
  145. Concatenate a list or tuple of words with intervening occurrences of
  146. <VAR>sep</VAR>.  The default value for <VAR>sep</VAR> is a single space character.
  147. It is always true that
  148. <CODE>string.join(string.split(<VAR>s</VAR>, <VAR>sep</VAR>), <VAR>sep</VAR>)</CODE>
  149. equals <VAR>s</VAR>.
  150. </DL>
  151. <DL><DT><B>joinfields</B> (<VAR>words</VAR>[, <VAR>sep</VAR>]) -- function of module string<DD>
  152. This function behaves identical to <CODE>join</CODE>.  (In the past,
  153. <CODE>join</CODE> was only used with one argument, while <CODE>joinfields</CODE>
  154. was only used with two arguments.)
  155. </DL>
  156. <DL><DT><B>lstrip</B> (<VAR>s</VAR>) -- function of module string<DD>
  157. Remove leading whitespace from the string <VAR>s</VAR>.
  158. </DL>
  159. <DL><DT><B>rstrip</B> (<VAR>s</VAR>) -- function of module string<DD>
  160. Remove trailing whitespace from the string <VAR>s</VAR>.
  161. </DL>
  162. <DL><DT><B>strip</B> (<VAR>s</VAR>) -- function of module string<DD>
  163. Remove leading and trailing whitespace from the string <VAR>s</VAR>.
  164. </DL>
  165. <DL><DT><B>swapcase</B> (<VAR>s</VAR>) -- function of module string<DD>
  166. Convert lower case letters to upper case and vice versa.
  167. </DL>
  168. <DL><DT><B>translate</B> (<VAR>s</VAR>, <VAR>table</VAR>[, <VAR>deletechars</VAR>]) -- function of module string<DD>
  169. Delete all characters from <VAR>s</VAR> that are in <VAR>deletechars</VAR> (if present), and 
  170. then translate the characters using <VAR>table</VAR>, which must be
  171. a 256-character string giving the translation for each character
  172. value, indexed by its ordinal.  
  173. </DL>
  174. <DL><DT><B>upper</B> (<VAR>s</VAR>) -- function of module string<DD>
  175. Convert letters to upper case.
  176. </DL>
  177. <DL><DT><B>ljust</B> (<VAR>s</VAR>, <VAR>width</VAR>) -- function of module string<DD>
  178. </DL>
  179. <DL><DT><B>rjust</B> (<VAR>s</VAR>, <VAR>width</VAR>) -- function of module string<DD>
  180. </DL>
  181. <DL><DT><B>center</B> (<VAR>s</VAR>, <VAR>width</VAR>) -- function of module string<DD>
  182. These functions respectively left-justify, right-justify and center a
  183. string in a field of given width.
  184. They return a string that is at least
  185. <VAR>width</VAR>
  186. characters wide, created by padding the string
  187. <VAR>s</VAR>
  188. with spaces until the given width on the right, left or both sides.
  189. The string is never truncated.
  190. </DL>
  191. <DL><DT><B>zfill</B> (<VAR>s</VAR>, <VAR>width</VAR>) -- function of module string<DD>
  192. Pad a numeric string on the left with zero digits until the given
  193. width is reached.  Strings starting with a sign are handled correctly.
  194. </DL>
  195. This module is implemented in Python.  Much of its functionality has
  196. been reimplemented in the built-in module <CODE>strop</CODE>.  However, you
  197. should <I>never</I> import the latter module directly.  When
  198. <CODE>string</CODE> discovers that <CODE>strop</CODE> exists, it transparently
  199. replaces parts of itself with the implementation from <CODE>strop</CODE>.
  200. After initialization, there is <I>no</I> overhead in using
  201. <CODE>string</CODE> instead of <CODE>strop</CODE>.
  202.