| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="typesseq-strings.html" /> |
| 13 | <link rel="prev" href="typesseq.html" /> |
| 14 | <link rel="parent" href="typesseq.html" /> |
| 15 | <link rel="next" href="typesseq-strings.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>2.3.6.1 String Methods </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="2.3.6 Sequence Types " |
| 25 | href="typesseq.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="2.3.6 Sequence Types " |
| 28 | href="typesseq.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="2.3.6.2 String Formatting Operations" |
| 31 | href="typesseq-strings.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="typesseq.html">2.3.6 Sequence Types </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="typesseq.html">2.3.6 Sequence Types </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="typesseq-strings.html">2.3.6.2 String Formatting Operations</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H3><A NAME="SECTION004361000000000000000"></A><A NAME="string-methods"></A> |
| 56 | <BR> |
| 57 | 2.3.6.1 String Methods |
| 58 | </H3> |
| 59 | |
| 60 | <P> |
| 61 | These are the string methods which both 8-bit strings and Unicode |
| 62 | objects support: |
| 63 | |
| 64 | <P> |
| 65 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 66 | <td><nobr><b><tt id='l2h-176' xml:id='l2h-176' class="method">capitalize</tt></b>(</nobr></td> |
| 67 | <td><var></var>)</td></tr></table></dt> |
| 68 | <dd> |
| 69 | Return a copy of the string with only its first character capitalized. |
| 70 | |
| 71 | <P> |
| 72 | For 8-bit strings, this method is locale-dependent. |
| 73 | </dl> |
| 74 | |
| 75 | <P> |
| 76 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 77 | <td><nobr><b><tt id='l2h-177' xml:id='l2h-177' class="method">center</tt></b>(</nobr></td> |
| 78 | <td><var>width</var><big>[</big><var>, fillchar</var><big>]</big><var></var>)</td></tr></table></dt> |
| 79 | <dd> |
| 80 | Return centered in a string of length <var>width</var>. Padding is done |
| 81 | using the specified <var>fillchar</var> (default is a space). |
| 82 | |
| 83 | <span class="versionnote">Changed in version 2.4: |
| 84 | Support for the <var>fillchar</var> argument.</span> |
| 85 | |
| 86 | </dl> |
| 87 | |
| 88 | <P> |
| 89 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 90 | <td><nobr><b><tt id='l2h-178' xml:id='l2h-178' class="method">count</tt></b>(</nobr></td> |
| 91 | <td><var>sub</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 92 | <dd> |
| 93 | Return the number of occurrences of substring <var>sub</var> in string |
| 94 | S<code>[<var>start</var>:<var>end</var>]</code>. Optional arguments <var>start</var> and |
| 95 | <var>end</var> are interpreted as in slice notation. |
| 96 | </dl> |
| 97 | |
| 98 | <P> |
| 99 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 100 | <td><nobr><b><tt id='l2h-179' xml:id='l2h-179' class="method">decode</tt></b>(</nobr></td> |
| 101 | <td><var></var><big>[</big><var>encoding</var><big>[</big><var>, errors</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 102 | <dd> |
| 103 | Decodes the string using the codec registered for <var>encoding</var>. |
| 104 | <var>encoding</var> defaults to the default string encoding. <var>errors</var> |
| 105 | may be given to set a different error handling scheme. The default is |
| 106 | <code>'strict'</code>, meaning that encoding errors raise |
| 107 | <tt class="exception">UnicodeError</tt>. Other possible values are <code>'ignore'</code>, |
| 108 | <code>'replace'</code> and any other name registered via |
| 109 | <tt class="function">codecs.register_error</tt>. |
| 110 | |
| 111 | <span class="versionnote">New in version 2.2.</span> |
| 112 | |
| 113 | <span class="versionnote">Changed in version 2.3: |
| 114 | Support for other error handling schemes added.</span> |
| 115 | |
| 116 | </dl> |
| 117 | |
| 118 | <P> |
| 119 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 120 | <td><nobr><b><tt id='l2h-180' xml:id='l2h-180' class="method">encode</tt></b>(</nobr></td> |
| 121 | <td><var></var><big>[</big><var>encoding</var><big>[</big><var>,errors</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 122 | <dd> |
| 123 | Return an encoded version of the string. Default encoding is the current |
| 124 | default string encoding. <var>errors</var> may be given to set a different |
| 125 | error handling scheme. The default for <var>errors</var> is |
| 126 | <code>'strict'</code>, meaning that encoding errors raise a |
| 127 | <tt class="exception">UnicodeError</tt>. Other possible values are <code>'ignore'</code>, |
| 128 | <code>'replace'</code>, <code>'xmlcharrefreplace'</code>, <code>'backslashreplace'</code> |
| 129 | and any other name registered via <tt class="function">codecs.register_error</tt>. |
| 130 | For a list of possible encodings, see section <A href="standard-encodings.html#standard-encodings">4.9.2</A>. |
| 131 | |
| 132 | <span class="versionnote">New in version 2.0.</span> |
| 133 | |
| 134 | <span class="versionnote">Changed in version 2.3: |
| 135 | Support for <code>'xmlcharrefreplace'</code> and |
| 136 | <code>'backslashreplace'</code> and other error handling schemes added.</span> |
| 137 | |
| 138 | </dl> |
| 139 | |
| 140 | <P> |
| 141 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 142 | <td><nobr><b><tt id='l2h-181' xml:id='l2h-181' class="method">endswith</tt></b>(</nobr></td> |
| 143 | <td><var>suffix</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 144 | <dd> |
| 145 | Return <code>True</code> if the string ends with the specified <var>suffix</var>, |
| 146 | otherwise return <code>False</code>. With optional <var>start</var>, test beginning at |
| 147 | that position. With optional <var>end</var>, stop comparing at that position. |
| 148 | </dl> |
| 149 | |
| 150 | <P> |
| 151 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 152 | <td><nobr><b><tt id='l2h-182' xml:id='l2h-182' class="method">expandtabs</tt></b>(</nobr></td> |
| 153 | <td><var></var><big>[</big><var>tabsize</var><big>]</big><var></var>)</td></tr></table></dt> |
| 154 | <dd> |
| 155 | Return a copy of the string where all tab characters are expanded |
| 156 | using spaces. If <var>tabsize</var> is not given, a tab size of <code>8</code> |
| 157 | characters is assumed. |
| 158 | </dl> |
| 159 | |
| 160 | <P> |
| 161 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 162 | <td><nobr><b><tt id='l2h-183' xml:id='l2h-183' class="method">find</tt></b>(</nobr></td> |
| 163 | <td><var>sub</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 164 | <dd> |
| 165 | Return the lowest index in the string where substring <var>sub</var> is |
| 166 | found, such that <var>sub</var> is contained in the range [<var>start</var>, |
| 167 | <var>end</var>). Optional arguments <var>start</var> and <var>end</var> are |
| 168 | interpreted as in slice notation. Return <code>-1</code> if <var>sub</var> is |
| 169 | not found. |
| 170 | </dl> |
| 171 | |
| 172 | <P> |
| 173 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 174 | <td><nobr><b><tt id='l2h-184' xml:id='l2h-184' class="method">index</tt></b>(</nobr></td> |
| 175 | <td><var>sub</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 176 | <dd> |
| 177 | Like <tt class="method">find()</tt>, but raise <tt class="exception">ValueError</tt> when the |
| 178 | substring is not found. |
| 179 | </dl> |
| 180 | |
| 181 | <P> |
| 182 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 183 | <td><nobr><b><tt id='l2h-185' xml:id='l2h-185' class="method">isalnum</tt></b>(</nobr></td> |
| 184 | <td><var></var>)</td></tr></table></dt> |
| 185 | <dd> |
| 186 | Return true if all characters in the string are alphanumeric and there |
| 187 | is at least one character, false otherwise. |
| 188 | |
| 189 | <P> |
| 190 | For 8-bit strings, this method is locale-dependent. |
| 191 | </dl> |
| 192 | |
| 193 | <P> |
| 194 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 195 | <td><nobr><b><tt id='l2h-186' xml:id='l2h-186' class="method">isalpha</tt></b>(</nobr></td> |
| 196 | <td><var></var>)</td></tr></table></dt> |
| 197 | <dd> |
| 198 | Return true if all characters in the string are alphabetic and there |
| 199 | is at least one character, false otherwise. |
| 200 | |
| 201 | <P> |
| 202 | For 8-bit strings, this method is locale-dependent. |
| 203 | </dl> |
| 204 | |
| 205 | <P> |
| 206 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 207 | <td><nobr><b><tt id='l2h-187' xml:id='l2h-187' class="method">isdigit</tt></b>(</nobr></td> |
| 208 | <td><var></var>)</td></tr></table></dt> |
| 209 | <dd> |
| 210 | Return true if all characters in the string are digits and there |
| 211 | is at least one character, false otherwise. |
| 212 | |
| 213 | <P> |
| 214 | For 8-bit strings, this method is locale-dependent. |
| 215 | </dl> |
| 216 | |
| 217 | <P> |
| 218 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 219 | <td><nobr><b><tt id='l2h-188' xml:id='l2h-188' class="method">islower</tt></b>(</nobr></td> |
| 220 | <td><var></var>)</td></tr></table></dt> |
| 221 | <dd> |
| 222 | Return true if all cased characters in the string are lowercase and |
| 223 | there is at least one cased character, false otherwise. |
| 224 | |
| 225 | <P> |
| 226 | For 8-bit strings, this method is locale-dependent. |
| 227 | </dl> |
| 228 | |
| 229 | <P> |
| 230 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 231 | <td><nobr><b><tt id='l2h-189' xml:id='l2h-189' class="method">isspace</tt></b>(</nobr></td> |
| 232 | <td><var></var>)</td></tr></table></dt> |
| 233 | <dd> |
| 234 | Return true if there are only whitespace characters in the string and |
| 235 | there is at least one character, false otherwise. |
| 236 | |
| 237 | <P> |
| 238 | For 8-bit strings, this method is locale-dependent. |
| 239 | </dl> |
| 240 | |
| 241 | <P> |
| 242 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 243 | <td><nobr><b><tt id='l2h-190' xml:id='l2h-190' class="method">istitle</tt></b>(</nobr></td> |
| 244 | <td><var></var>)</td></tr></table></dt> |
| 245 | <dd> |
| 246 | Return true if the string is a titlecased string and there is at least one |
| 247 | character, for example uppercase characters may only follow uncased |
| 248 | characters and lowercase characters only cased ones. Return false |
| 249 | otherwise. |
| 250 | |
| 251 | <P> |
| 252 | For 8-bit strings, this method is locale-dependent. |
| 253 | </dl> |
| 254 | |
| 255 | <P> |
| 256 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 257 | <td><nobr><b><tt id='l2h-191' xml:id='l2h-191' class="method">isupper</tt></b>(</nobr></td> |
| 258 | <td><var></var>)</td></tr></table></dt> |
| 259 | <dd> |
| 260 | Return true if all cased characters in the string are uppercase and |
| 261 | there is at least one cased character, false otherwise. |
| 262 | |
| 263 | <P> |
| 264 | For 8-bit strings, this method is locale-dependent. |
| 265 | </dl> |
| 266 | |
| 267 | <P> |
| 268 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 269 | <td><nobr><b><tt id='l2h-192' xml:id='l2h-192' class="method">join</tt></b>(</nobr></td> |
| 270 | <td><var>seq</var>)</td></tr></table></dt> |
| 271 | <dd> |
| 272 | Return a string which is the concatenation of the strings in the |
| 273 | sequence <var>seq</var>. The separator between elements is the string |
| 274 | providing this method. |
| 275 | </dl> |
| 276 | |
| 277 | <P> |
| 278 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 279 | <td><nobr><b><tt id='l2h-193' xml:id='l2h-193' class="method">ljust</tt></b>(</nobr></td> |
| 280 | <td><var>width</var><big>[</big><var>, fillchar</var><big>]</big><var></var>)</td></tr></table></dt> |
| 281 | <dd> |
| 282 | Return the string left justified in a string of length <var>width</var>. |
| 283 | Padding is done using the specified <var>fillchar</var> (default is a |
| 284 | space). The original string is returned if |
| 285 | <var>width</var> is less than <code>len(<var>s</var>)</code>. |
| 286 | |
| 287 | <span class="versionnote">Changed in version 2.4: |
| 288 | Support for the <var>fillchar</var> argument.</span> |
| 289 | |
| 290 | </dl> |
| 291 | |
| 292 | <P> |
| 293 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 294 | <td><nobr><b><tt id='l2h-194' xml:id='l2h-194' class="method">lower</tt></b>(</nobr></td> |
| 295 | <td><var></var>)</td></tr></table></dt> |
| 296 | <dd> |
| 297 | Return a copy of the string converted to lowercase. |
| 298 | |
| 299 | <P> |
| 300 | For 8-bit strings, this method is locale-dependent. |
| 301 | </dl> |
| 302 | |
| 303 | <P> |
| 304 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 305 | <td><nobr><b><tt id='l2h-195' xml:id='l2h-195' class="method">lstrip</tt></b>(</nobr></td> |
| 306 | <td><var></var><big>[</big><var>chars</var><big>]</big><var></var>)</td></tr></table></dt> |
| 307 | <dd> |
| 308 | Return a copy of the string with leading characters removed. The |
| 309 | <var>chars</var> argument is a string specifying the set of characters |
| 310 | to be removed. If omitted or <code>None</code>, the <var>chars</var> argument |
| 311 | defaults to removing whitespace. The <var>chars</var> argument is not |
| 312 | a prefix; rather, all combinations of its values are stripped: |
| 313 | <div class="verbatim"><pre> |
| 314 | >>> ' spacious '.lstrip() |
| 315 | 'spacious ' |
| 316 | >>> 'www.example.com'.lstrip('cmowz.') |
| 317 | 'example.com' |
| 318 | </pre></div> |
| 319 | |
| 320 | <span class="versionnote">Changed in version 2.2.2: |
| 321 | Support for the <var>chars</var> argument.</span> |
| 322 | |
| 323 | </dl> |
| 324 | |
| 325 | <P> |
| 326 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 327 | <td><nobr><b><tt id='l2h-196' xml:id='l2h-196' class="method">replace</tt></b>(</nobr></td> |
| 328 | <td><var>old, new</var><big>[</big><var>, count</var><big>]</big><var></var>)</td></tr></table></dt> |
| 329 | <dd> |
| 330 | Return a copy of the string with all occurrences of substring |
| 331 | <var>old</var> replaced by <var>new</var>. If the optional argument |
| 332 | <var>count</var> is given, only the first <var>count</var> occurrences are |
| 333 | replaced. |
| 334 | </dl> |
| 335 | |
| 336 | <P> |
| 337 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 338 | <td><nobr><b><tt id='l2h-197' xml:id='l2h-197' class="method">rfind</tt></b>(</nobr></td> |
| 339 | <td><var>sub </var><big>[</big><var>,start </var><big>[</big><var>,end</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 340 | <dd> |
| 341 | Return the highest index in the string where substring <var>sub</var> is |
| 342 | found, such that <var>sub</var> is contained within s[start,end]. Optional |
| 343 | arguments <var>start</var> and <var>end</var> are interpreted as in slice |
| 344 | notation. Return <code>-1</code> on failure. |
| 345 | </dl> |
| 346 | |
| 347 | <P> |
| 348 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 349 | <td><nobr><b><tt id='l2h-198' xml:id='l2h-198' class="method">rindex</tt></b>(</nobr></td> |
| 350 | <td><var>sub</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 351 | <dd> |
| 352 | Like <tt class="method">rfind()</tt> but raises <tt class="exception">ValueError</tt> when the |
| 353 | substring <var>sub</var> is not found. |
| 354 | </dl> |
| 355 | |
| 356 | <P> |
| 357 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 358 | <td><nobr><b><tt id='l2h-199' xml:id='l2h-199' class="method">rjust</tt></b>(</nobr></td> |
| 359 | <td><var>width</var><big>[</big><var>, fillchar</var><big>]</big><var></var>)</td></tr></table></dt> |
| 360 | <dd> |
| 361 | Return the string right justified in a string of length <var>width</var>. |
| 362 | Padding is done using the specified <var>fillchar</var> (default is a space). |
| 363 | The original string is returned if |
| 364 | <var>width</var> is less than <code>len(<var>s</var>)</code>. |
| 365 | |
| 366 | <span class="versionnote">Changed in version 2.4: |
| 367 | Support for the <var>fillchar</var> argument.</span> |
| 368 | |
| 369 | </dl> |
| 370 | |
| 371 | <P> |
| 372 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 373 | <td><nobr><b><tt id='l2h-200' xml:id='l2h-200' class="method">rsplit</tt></b>(</nobr></td> |
| 374 | <td><var></var><big>[</big><var>sep </var><big>[</big><var>,maxsplit</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 375 | <dd> |
| 376 | Return a list of the words in the string, using <var>sep</var> as the |
| 377 | delimiter string. If <var>maxsplit</var> is given, at most <var>maxsplit</var> |
| 378 | splits are done, the <em>rightmost</em> ones. If <var>sep</var> is not specified |
| 379 | or <code>None</code>, any whitespace string is a separator. Except for splitting |
| 380 | from the right, <tt class="method">rsplit()</tt> behaves like <tt class="method">split()</tt> which |
| 381 | is described in detail below. |
| 382 | |
| 383 | <span class="versionnote">New in version 2.4.</span> |
| 384 | |
| 385 | </dl> |
| 386 | |
| 387 | <P> |
| 388 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 389 | <td><nobr><b><tt id='l2h-201' xml:id='l2h-201' class="method">rstrip</tt></b>(</nobr></td> |
| 390 | <td><var></var><big>[</big><var>chars</var><big>]</big><var></var>)</td></tr></table></dt> |
| 391 | <dd> |
| 392 | Return a copy of the string with trailing characters removed. The |
| 393 | <var>chars</var> argument is a string specifying the set of characters |
| 394 | to be removed. If omitted or <code>None</code>, the <var>chars</var> argument |
| 395 | defaults to removing whitespace. The <var>chars</var> argument is not |
| 396 | a suffix; rather, all combinations of its values are stripped: |
| 397 | <div class="verbatim"><pre> |
| 398 | >>> ' spacious '.rstrip() |
| 399 | ' spacious' |
| 400 | >>> 'mississippi'.rstrip('ipz') |
| 401 | 'mississ' |
| 402 | </pre></div> |
| 403 | |
| 404 | <span class="versionnote">Changed in version 2.2.2: |
| 405 | Support for the <var>chars</var> argument.</span> |
| 406 | |
| 407 | </dl> |
| 408 | |
| 409 | <P> |
| 410 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 411 | <td><nobr><b><tt id='l2h-202' xml:id='l2h-202' class="method">split</tt></b>(</nobr></td> |
| 412 | <td><var></var><big>[</big><var>sep </var><big>[</big><var>,maxsplit</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 413 | <dd> |
| 414 | Return a list of the words in the string, using <var>sep</var> as the |
| 415 | delimiter string. If <var>maxsplit</var> is given, at most <var>maxsplit</var> |
| 416 | splits are done. (thus, the list will have at most <code><var>maxsplit</var>+1</code> |
| 417 | elements). If <var>maxsplit</var> is not specified, then there |
| 418 | is no limit on the number of splits (all possible splits are made). |
| 419 | Consecutive delimiters are not grouped together and are |
| 420 | deemed to delimit empty strings (for example, "<tt class="samp">'1,,2'.split(',')</tt>"returns "<tt class="samp">['1', '', '2']</tt>"). The <var>sep</var> argument may consist of |
| 421 | multiple characters (for example, "<tt class="samp">'1, 2, 3'.split(', ')</tt>" returns |
| 422 | "<tt class="samp">['1', '2', '3']</tt>"). Splitting an empty string with a specified |
| 423 | separator returns "<tt class="samp">['']</tt>". |
| 424 | |
| 425 | <P> |
| 426 | If <var>sep</var> is not specified or is <code>None</code>, a different splitting |
| 427 | algorithm is applied. First, whitespace characters (spaces, tabs, |
| 428 | newlines, returns, and formfeeds) are stripped from both ends. Then, |
| 429 | words are separated by arbitrary length strings of whitespace |
| 430 | characters. Consecutive whitespace delimiters are treated as a single |
| 431 | delimiter ("<tt class="samp">'1 2 3'.split()</tt>" returns "<tt class="samp">['1', '2', '3']</tt>"). |
| 432 | Splitting an empty string or a string consisting of just whitespace |
| 433 | returns an empty list. |
| 434 | </dl> |
| 435 | |
| 436 | <P> |
| 437 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 438 | <td><nobr><b><tt id='l2h-203' xml:id='l2h-203' class="method">splitlines</tt></b>(</nobr></td> |
| 439 | <td><var></var><big>[</big><var>keepends</var><big>]</big><var></var>)</td></tr></table></dt> |
| 440 | <dd> |
| 441 | Return a list of the lines in the string, breaking at line |
| 442 | boundaries. Line breaks are not included in the resulting list unless |
| 443 | <var>keepends</var> is given and true. |
| 444 | </dl> |
| 445 | |
| 446 | <P> |
| 447 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 448 | <td><nobr><b><tt id='l2h-204' xml:id='l2h-204' class="method">startswith</tt></b>(</nobr></td> |
| 449 | <td><var>prefix</var><big>[</big><var>, |
| 450 | start</var><big>[</big><var>, end</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 451 | <dd> |
| 452 | Return <code>True</code> if string starts with the <var>prefix</var>, otherwise |
| 453 | return <code>False</code>. With optional <var>start</var>, test string beginning at |
| 454 | that position. With optional <var>end</var>, stop comparing string at that |
| 455 | position. |
| 456 | </dl> |
| 457 | |
| 458 | <P> |
| 459 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 460 | <td><nobr><b><tt id='l2h-205' xml:id='l2h-205' class="method">strip</tt></b>(</nobr></td> |
| 461 | <td><var></var><big>[</big><var>chars</var><big>]</big><var></var>)</td></tr></table></dt> |
| 462 | <dd> |
| 463 | Return a copy of the string with the leading and trailing characters |
| 464 | removed. The <var>chars</var> argument is a string specifying the set of |
| 465 | characters to be removed. If omitted or <code>None</code>, the <var>chars</var> |
| 466 | argument defaults to removing whitespace. The <var>chars</var> argument is not |
| 467 | a prefix or suffix; rather, all combinations of its values are stripped: |
| 468 | <div class="verbatim"><pre> |
| 469 | >>> ' spacious '.strip() |
| 470 | 'spacious' |
| 471 | >>> 'www.example.com'.strip('cmowz.') |
| 472 | 'example' |
| 473 | </pre></div> |
| 474 | |
| 475 | <span class="versionnote">Changed in version 2.2.2: |
| 476 | Support for the <var>chars</var> argument.</span> |
| 477 | |
| 478 | </dl> |
| 479 | |
| 480 | <P> |
| 481 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 482 | <td><nobr><b><tt id='l2h-206' xml:id='l2h-206' class="method">swapcase</tt></b>(</nobr></td> |
| 483 | <td><var></var>)</td></tr></table></dt> |
| 484 | <dd> |
| 485 | Return a copy of the string with uppercase characters converted to |
| 486 | lowercase and vice versa. |
| 487 | |
| 488 | <P> |
| 489 | For 8-bit strings, this method is locale-dependent. |
| 490 | </dl> |
| 491 | |
| 492 | <P> |
| 493 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 494 | <td><nobr><b><tt id='l2h-207' xml:id='l2h-207' class="method">title</tt></b>(</nobr></td> |
| 495 | <td><var></var>)</td></tr></table></dt> |
| 496 | <dd> |
| 497 | Return a titlecased version of the string: words start with uppercase |
| 498 | characters, all remaining cased characters are lowercase. |
| 499 | |
| 500 | <P> |
| 501 | For 8-bit strings, this method is locale-dependent. |
| 502 | </dl> |
| 503 | |
| 504 | <P> |
| 505 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 506 | <td><nobr><b><tt id='l2h-208' xml:id='l2h-208' class="method">translate</tt></b>(</nobr></td> |
| 507 | <td><var>table</var><big>[</big><var>, deletechars</var><big>]</big><var></var>)</td></tr></table></dt> |
| 508 | <dd> |
| 509 | Return a copy of the string where all characters occurring in the |
| 510 | optional argument <var>deletechars</var> are removed, and the remaining |
| 511 | characters have been mapped through the given translation table, which |
| 512 | must be a string of length 256. |
| 513 | |
| 514 | <P> |
| 515 | For Unicode objects, the <tt class="method">translate()</tt> method does not |
| 516 | accept the optional <var>deletechars</var> argument. Instead, it |
| 517 | returns a copy of the <var>s</var> where all characters have been mapped |
| 518 | through the given translation table which must be a mapping of |
| 519 | Unicode ordinals to Unicode ordinals, Unicode strings or <code>None</code>. |
| 520 | Unmapped characters are left untouched. Characters mapped to <code>None</code> |
| 521 | are deleted. Note, a more flexible approach is to create a custom |
| 522 | character mapping codec using the <tt class="module"><a href="module-codecs.html">codecs</a></tt> module (see |
| 523 | <tt class="module">encodings.cp1251</tt> for an example). |
| 524 | </dl> |
| 525 | |
| 526 | <P> |
| 527 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 528 | <td><nobr><b><tt id='l2h-209' xml:id='l2h-209' class="method">upper</tt></b>(</nobr></td> |
| 529 | <td><var></var>)</td></tr></table></dt> |
| 530 | <dd> |
| 531 | Return a copy of the string converted to uppercase. |
| 532 | |
| 533 | <P> |
| 534 | For 8-bit strings, this method is locale-dependent. |
| 535 | </dl> |
| 536 | |
| 537 | <P> |
| 538 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 539 | <td><nobr><b><tt id='l2h-210' xml:id='l2h-210' class="method">zfill</tt></b>(</nobr></td> |
| 540 | <td><var>width</var>)</td></tr></table></dt> |
| 541 | <dd> |
| 542 | Return the numeric string left filled with zeros in a string |
| 543 | of length <var>width</var>. The original string is returned if |
| 544 | <var>width</var> is less than <code>len(<var>s</var>)</code>. |
| 545 | |
| 546 | <span class="versionnote">New in version 2.2.2.</span> |
| 547 | |
| 548 | </dl> |
| 549 | |
| 550 | <P> |
| 551 | |
| 552 | <DIV CLASS="navigation"> |
| 553 | <div class='online-navigation'> |
| 554 | <p></p><hr /> |
| 555 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 556 | <tr> |
| 557 | <td class='online-navigation'><a rel="prev" title="2.3.6 Sequence Types " |
| 558 | href="typesseq.html"><img src='../icons/previous.png' |
| 559 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 560 | <td class='online-navigation'><a rel="parent" title="2.3.6 Sequence Types " |
| 561 | href="typesseq.html"><img src='../icons/up.png' |
| 562 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 563 | <td class='online-navigation'><a rel="next" title="2.3.6.2 String Formatting Operations" |
| 564 | href="typesseq-strings.html"><img src='../icons/next.png' |
| 565 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 566 | <td align="center" width="100%">Python Library Reference</td> |
| 567 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 568 | href="contents.html"><img src='../icons/contents.png' |
| 569 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 570 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 571 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 572 | <td class='online-navigation'><a rel="index" title="Index" |
| 573 | href="genindex.html"><img src='../icons/index.png' |
| 574 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 575 | </tr></table> |
| 576 | <div class='online-navigation'> |
| 577 | <b class="navlabel">Previous:</b> |
| 578 | <a class="sectref" rel="prev" href="typesseq.html">2.3.6 Sequence Types </A> |
| 579 | <b class="navlabel">Up:</b> |
| 580 | <a class="sectref" rel="parent" href="typesseq.html">2.3.6 Sequence Types </A> |
| 581 | <b class="navlabel">Next:</b> |
| 582 | <a class="sectref" rel="next" href="typesseq-strings.html">2.3.6.2 String Formatting Operations</A> |
| 583 | </div> |
| 584 | </div> |
| 585 | <hr /> |
| 586 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 587 | </DIV> |
| 588 | <!--End of Navigation Panel--> |
| 589 | <ADDRESS> |
| 590 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 591 | </ADDRESS> |
| 592 | </BODY> |
| 593 | </HTML> |