📄 module-string.html
字号:
Return the lowest index in <var>s</var> where the substring <var>sub</var> is
found such that <var>sub</var> is wholly contained in
<code><var>s</var>[<var>start</var>:<var>end</var>]</code>. Return <code>-1</code> on failure.
Defaults for <var>start</var> and <var>end</var> and interpretation of
negative values is the same as for slices.
</dl>
<P>
<dl><dt><b><a name='l2h-559'><tt class='function'>rfind</tt></a></b> (<var>s, sub</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><big>]</big>)
<dd>
Like <tt class="function">find()</tt> but find the highest index.
</dl>
<P>
<dl><dt><b><a name='l2h-560'><tt class='function'>index</tt></a></b> (<var>s, sub</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><big>]</big>)
<dd>
Like <tt class="function">find()</tt> but raise <tt class="exception">ValueError</tt> when the
substring is not found.
</dl>
<P>
<dl><dt><b><a name='l2h-561'><tt class='function'>rindex</tt></a></b> (<var>s, sub</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><big>]</big>)
<dd>
Like <tt class="function">rfind()</tt> but raise <tt class="exception">ValueError</tt> when the
substring is not found.
</dl>
<P>
<dl><dt><b><a name='l2h-562'><tt class='function'>count</tt></a></b> (<var>s, sub</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><big>]</big>)
<dd>
Return the number of (non-overlapping) occurrences of substring
<var>sub</var> in string <code><var>s</var>[<var>start</var>:<var>end</var>]</code>.
Defaults for <var>start</var> and <var>end</var> and interpretation of
negative values are the same as for slices.
</dl>
<P>
<dl><dt><b><a name='l2h-563'><tt class='function'>lower</tt></a></b> (<var>s</var>)
<dd>
Return a copy of <var>s</var>, but with upper case letters converted to
lower case.
</dl>
<P>
<dl><dt><b><a name='l2h-564'><tt class='function'>maketrans</tt></a></b> (<var>from, to</var>)
<dd>
Return a translation table suitable for passing to
<tt class="function">translate()</tt> or <tt class="function">regex.compile()</tt>, that will map
each character in <var>from</var> into the character at the same position
in <var>to</var>; <var>from</var> and <var>to</var> must have the same length.
<P>
<b>Warning:</b> don't use strings derived from <tt class="constant">lowercase</tt>
and <tt class="constant">uppercase</tt> as arguments; in some locales, these don't have
the same length. For case conversions, always use
<tt class="function">lower()</tt> and <tt class="function">upper()</tt>.
</dl>
<P>
<dl><dt><b><a name='l2h-565'><tt class='function'>split</tt></a></b> (<var>s</var><big>[</big><var>, sep</var><big>[</big><var>, maxsplit</var><big>]</big><big>]</big>)
<dd>
Return a list of the words of the string <var>s</var>. If the optional
second argument <var>sep</var> is absent or <code>None</code>, the words are
separated by arbitrary strings of whitespace characters (space, tab,
newline, return, formfeed). If the second argument <var>sep</var> is
present and not <code>None</code>, it specifies a string to be used as the
word separator. The returned list will then have one more item
than the number of non-overlapping occurrences of the separator in
the string. The optional third argument <var>maxsplit</var> defaults to
0. If it is nonzero, at most <var>maxsplit</var> number of splits occur,
and the remainder of the string is returned as the final element of
the list (thus, the list will have at most <code><var>maxsplit</var>+1</code>
elements).
</dl>
<P>
<dl><dt><b><a name='l2h-566'><tt class='function'>splitfields</tt></a></b> (<var>s</var><big>[</big><var>, sep</var><big>[</big><var>, maxsplit</var><big>]</big><big>]</big>)
<dd>
This function behaves identically to <tt class="function">split()</tt>. (In the
past, <tt class="function">split()</tt> was only used with one argument, while
<tt class="function">splitfields()</tt> was only used with two arguments.)
</dl>
<P>
<dl><dt><b><a name='l2h-567'><tt class='function'>join</tt></a></b> (<var>words</var><big>[</big><var>, sep</var><big>]</big>)
<dd>
Concatenate a list or tuple of words with intervening occurrences of
<var>sep</var>. The default value for <var>sep</var> is a single space
character. It is always true that
"<tt class="samp">string.join(string.split(<var>s</var>, <var>sep</var>), <var>sep</var>)</tt>" equals <var>s</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-568'><tt class='function'>joinfields</tt></a></b> (<var>words</var><big>[</big><var>, sep</var><big>]</big>)
<dd>
This function behaves identical to <tt class="function">join()</tt>. (In the past,
<tt class="function">join()</tt> was only used with one argument, while
<tt class="function">joinfields()</tt> was only used with two arguments.)
</dl>
<P>
<dl><dt><b><a name='l2h-569'><tt class='function'>lstrip</tt></a></b> (<var>s</var>)
<dd>
Return a copy of <var>s</var> but without leading whitespace characters.
</dl>
<P>
<dl><dt><b><a name='l2h-570'><tt class='function'>rstrip</tt></a></b> (<var>s</var>)
<dd>
Return a copy of <var>s</var> but without trailing whitespace
characters.
</dl>
<P>
<dl><dt><b><a name='l2h-571'><tt class='function'>strip</tt></a></b> (<var>s</var>)
<dd>
Return a copy of <var>s</var> without leading or trailing whitespace.
</dl>
<P>
<dl><dt><b><a name='l2h-572'><tt class='function'>swapcase</tt></a></b> (<var>s</var>)
<dd>
Return a copy of <var>s</var>, but with lower case letters
converted to upper case and vice versa.
</dl>
<P>
<dl><dt><b><a name='l2h-573'><tt class='function'>translate</tt></a></b> (<var>s, table</var><big>[</big><var>, deletechars</var><big>]</big>)
<dd>
Delete all characters from <var>s</var> that are in <var>deletechars</var> (if
present), and then translate the characters using <var>table</var>, which
must be a 256-character string giving the translation for each
character value, indexed by its ordinal.
</dl>
<P>
<dl><dt><b><a name='l2h-574'><tt class='function'>upper</tt></a></b> (<var>s</var>)
<dd>
Return a copy of <var>s</var>, but with lower case letters converted to
upper case.
</dl>
<P>
<dl><dt><b><a name='l2h-575'><tt class='function'>ljust</tt></a></b> (<var>s, width</var>)
<dd>
<dt><b><tt class='function'>rjust</tt></b> (<var>s, width</var>)
<dd>
<dt><b><tt class='function'>center</tt></b> (<var>s, width</var>)
<dd>
These functions respectively left-justify, right-justify and center
a string in a field of given width. They return a string that is at
least <var>width</var> characters wide, created by padding the string
<var>s</var> with spaces until the given width on the right, left or both
sides. The string is never truncated.
</dl>
<P>
<dl><dt><b><a name='l2h-576'><tt class='function'>zfill</tt></a></b> (<var>s, width</var>)
<dd>
Pad a numeric string on the left with zero digits until the given
width is reached. Strings starting with a sign are handled
correctly.
</dl>
<P>
<dl><dt><b><a name='l2h-577'><tt class='function'>replace</tt></a></b> (<var>str, old, new</var><big>[</big><var>, maxsplit</var><big>]</big>)
<dd>
Return a copy of string <var>str</var> with all occurrences of substring
<var>old</var> replaced by <var>new</var>. If the optional argument
<var>maxsplit</var> is given, the first <var>maxsplit</var> occurrences are
replaced.
</dl>
<P>
This module is implemented in Python. Much of its functionality has
been reimplemented in the built-in module
<tt class="module">strop</tt>. However, you
should <i>never</i> import the latter module directly. When
<tt class="module">string</tt> discovers that <tt class="module">strop</tt> exists, it transparently
replaces parts of itself with the implementation from <tt class="module">strop</tt>.
After initialization, there is <i>no</i> overhead in using
<tt class="module">string</tt> instead of <tt class="module">strop</tt>.
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="strings.html" tppabs="http://www.python.org/doc/current/lib/strings.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="strings.html" tppabs="http://www.python.org/doc/current/lib/strings.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="strings.html" tppabs="http://www.python.org/doc/current/lib/strings.html">4. String Services</A>
<b class="navlabel">Up:</b> <a class="sectref" href="strings.html" tppabs="http://www.python.org/doc/current/lib/strings.html">4. String Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.html">4.2 re </A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -