📄 string-methods.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>2.1.5.1 String Methods </title>
<META NAME="description" CONTENT="2.1.5.1 String Methods ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="typesseq-strings.html" tppabs="http://www.python.org/doc/current/lib/typesseq-strings.html">
<LINK REL="previous" href="typesseq.html" tppabs="http://www.python.org/doc/current/lib/typesseq.html">
<LINK REL="up" href="typesseq.html" tppabs="http://www.python.org/doc/current/lib/typesseq.html">
<LINK REL="next" href="typesseq-strings.html" tppabs="http://www.python.org/doc/current/lib/typesseq-strings.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="typesseq.html" tppabs="http://www.python.org/doc/current/lib/typesseq.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="typesseq.html" tppabs="http://www.python.org/doc/current/lib/typesseq.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="typesseq-strings.html" tppabs="http://www.python.org/doc/current/lib/typesseq-strings.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="typesseq.html" tppabs="http://www.python.org/doc/current/lib/typesseq.html">2.1.5 Sequence Types</A>
<b class="navlabel">Up:</b> <a class="sectref" href="typesseq.html" tppabs="http://www.python.org/doc/current/lib/typesseq.html">2.1.5 Sequence Types</A>
<b class="navlabel">Next:</b> <a class="sectref" href="typesseq-strings.html" tppabs="http://www.python.org/doc/current/lib/typesseq-strings.html">2.1.5.2 String Formatting Operations</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H3>
<BR>
2.1.5.1 String Methods
</H3>
<P>
These are the string methods which both 8-bit strings and Unicode
objects support:
<P>
<dl><dt><b><a name='l2h-36'><tt class='method'>capitalize</tt></a></b> ()
<dd>
Return a copy of the string with only its first character capitalized.
</dl>
<P>
<dl><dt><b><a name='l2h-37'><tt class='method'>center</tt></a></b> (<var>width</var>)
<dd>
Return centered in a string of length <var>width</var>. Padding is done
using spaces.
</dl>
<P>
<dl><dt><b><a name='l2h-38'><tt class='method'>count</tt></a></b> (<var>sub</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><big>]</big>)
<dd>
Return the number of occurrences of substring <var>sub</var> in string
S<code>[<var>start</var>:<var>end</var>]</code>. Optional arguments <var>start</var> and
<var>end</var> are interpreted as in slice notation.
</dl>
<P>
<dl><dt><b><a name='l2h-39'><tt class='method'>encode</tt></a></b> (<big>[</big><var>encoding</var><big>[</big><var>,errors</var><big>]</big><big>]</big>)
<dd>
Return an encoded version of the string. Default encoding is the current
default string encoding. <var>errors</var> may be given to set a different
error handling scheme. The default for <var>errors</var> is
<code>'strict'</code>, meaning that encoding errors raise a
<tt class="exception">ValueError</tt>. Other possible values are <code>'ignore'</code> and
<code>'replace'</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-40'><tt class='method'>endswith</tt></a></b> (<var>suffix</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><big>]</big>)
<dd>
Return true if the string ends with the specified <var>suffix</var>,
otherwise return false. With optional <var>start</var>, test beginning at
that position. With optional <var>end</var>, stop comparing at that position.
</dl>
<P>
<dl><dt><b><a name='l2h-41'><tt class='method'>expandtabs</tt></a></b> (<big>[</big><var>tabsize</var><big>]</big>)
<dd>
Return a copy of the string where all tab characters are expanded
using spaces. If <var>tabsize</var> is not given, a tab size of <code>8</code>
characters is assumed.
</dl>
<P>
<dl><dt><b><a name='l2h-42'><tt class='method'>find</tt></a></b> (<var>sub</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><big>]</big>)
<dd>
Return the lowest index in the string where substring <var>sub</var> is
found, such that <var>sub</var> is contained in the range [<var>start</var>,
<var>end</var>). Optional arguments <var>start</var> and <var>end</var> are
interpreted as in slice notation. Return <code>-1</code> if <var>sub</var> is
not found.
</dl>
<P>
<dl><dt><b><a name='l2h-43'><tt class='method'>index</tt></a></b> (<var>sub</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><big>]</big>)
<dd>
Like <tt class="method">find()</tt>, but raise <tt class="exception">ValueError</tt> when the
substring is not found.
</dl>
<P>
<dl><dt><b><a name='l2h-44'><tt class='method'>isalnum</tt></a></b> ()
<dd>
Return true if all characters in the string are alphanumeric and there
is at least one character, false otherwise.
</dl>
<P>
<dl><dt><b><a name='l2h-45'><tt class='method'>isalpha</tt></a></b> ()
<dd>
Return true if all characters in the string are alphabetic and there
is at least one character, false otherwise.
</dl>
<P>
<dl><dt><b><a name='l2h-46'><tt class='method'>isdigit</tt></a></b> ()
<dd>
Return true if there are only digit characters, false otherwise.
</dl>
<P>
<dl><dt><b><a name='l2h-47'><tt class='method'>islower</tt></a></b> ()
<dd>
Return true if all cased characters in the string are lowercase and
there is at least one cased character, false otherwise.
</dl>
<P>
<dl><dt><b><a name='l2h-48'><tt class='method'>isspace</tt></a></b> ()
<dd>
Return true if there are only whitespace characters in the string and
the string is not empty, false otherwise.
</dl>
<P>
<dl><dt><b><a name='l2h-49'><tt class='method'>istitle</tt></a></b> ()
<dd>
Return true if the string is a titlecased string, i.e. uppercase
characters may only follow uncased characters and lowercase characters
only cased ones. Return false otherwise.
</dl>
<P>
<dl><dt><b><a name='l2h-50'><tt class='method'>isupper</tt></a></b> ()
<dd>
Return true if all cased characters in the string are uppercase and
there is at least one cased character, false otherwise.
</dl>
<P>
<dl><dt><b><a name='l2h-51'><tt class='method'>join</tt></a></b> (<var>seq</var>)
<dd>
Return a string which is the concatenation of the strings in the
sequence <var>seq</var>. The separator between elements is the string
providing this method.
</dl>
<P>
<dl><dt><b><a name='l2h-52'><tt class='method'>ljust</tt></a></b> (<var>width</var>)
<dd>
Return the string left justified in a string of length <var>width</var>.
Padding is done using spaces. The original string is returned if
<var>width</var> is less than <code>len(<var>s</var>)</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-53'><tt class='method'>lower</tt></a></b> ()
<dd>
Return a copy of the string converted to lowercase.
</dl>
<P>
<dl><dt><b><a name='l2h-54'><tt class='method'>lstrip</tt></a></b> ()
<dd>
Return a copy of the string with leading whitespace removed.
</dl>
<P>
<dl><dt><b><a name='l2h-55'><tt class='method'>replace</tt></a></b> (<var>old, new</var><big>[</big><var>, maxsplit</var><big>]</big>)
<dd>
Return a copy of the string with all occurrences of substring
<var>old</var> replaced by <var>new</var>. If the optional argument
<var>maxsplit</var> is given, only the first <var>maxsplit</var> occurrences are
replaced.
</dl>
<P>
<dl><dt><b><a name='l2h-56'><tt class='method'>rfind</tt></a></b> (<var>sub </var><big>[</big><var>,start </var><big>[</big><var>,end</var><big>]</big><big>]</big>)
<dd>
Return the highest index in the string where substring <var>sub</var> is
found, such that <var>sub</var> is contained within s[start,end]. Optional
arguments <var>start</var> and <var>end</var> are interpreted as in slice
notation. Return <code>-1</code> on failure.
</dl>
<P>
<dl><dt><b><a name='l2h-57'><tt class='method'>rindex</tt></a></b> (<var>sub</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><big>]</big>)
<dd>
Like <tt class="method">rfind()</tt> but raises <tt class="exception">ValueError</tt> when the
substring <var>sub</var> is not found.
</dl>
<P>
<dl><dt><b><a name='l2h-58'><tt class='method'>rjust</tt></a></b> (<var>width</var>)
<dd>
Return the string right justified in a string of length <var>width</var>.
Padding is done using spaces. The original string is returned if
<var>width</var> is less than <code>len(<var>s</var>)</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-59'><tt class='method'>rstrip</tt></a></b> ()
<dd>
Return a copy of the string with trailing whitespace removed.
</dl>
<P>
<dl><dt><b><a name='l2h-60'><tt class='method'>split</tt></a></b> (<big>[</big><var>sep </var><big>[</big><var>,maxsplit</var><big>]</big><big>]</big>)
<dd>
Return a list of the words in the string, using <var>sep</var> as the
delimiter string. If <var>maxsplit</var> is given, at most <var>maxsplit</var>
splits are done. If <var>sep</var> is not specified or <code>None</code>, any
whitespace string is a separator.
</dl>
<P>
<dl><dt><b><a name='l2h-61'><tt class='method'>splitlines</tt></a></b> (<big>[</big><var>keepends</var><big>]</big>)
<dd>
Return a list of the lines in the string, breaking at line
boundaries. Line breaks are not included in the resulting list unless
<var>keepends</var> is given and true.
</dl>
<P>
<dl><dt><b><a name='l2h-62'><tt class='method'>startswith</tt></a></b> (<var>prefix</var><big>[</big><var>, start</var><big>[</big><var>, end</var><big>]</big><big>]</big>)
<dd>
Return true if string starts with the <var>prefix</var>, otherwise
return false. With optional <var>start</var>, test string beginning at
that position. With optional <var>end</var>, stop comparing string at that
position.
</dl>
<P>
<dl><dt><b><a name='l2h-63'><tt class='method'>strip</tt></a></b> ()
<dd>
Return a copy of the string with leading and trailing whitespace
removed.
</dl>
<P>
<dl><dt><b><a name='l2h-64'><tt class='method'>swapcase</tt></a></b> ()
<dd>
Return a copy of the string with uppercase characters converted to
lowercase and vice versa.
</dl>
<P>
<dl><dt><b><a name='l2h-65'><tt class='method'>title</tt></a></b> ()
<dd>
Return a titlecased version of, i.e. words start with uppercase
characters, all remaining cased characters are lowercase.
</dl>
<P>
<dl><dt><b><a name='l2h-66'><tt class='method'>translate</tt></a></b> (<var>table</var><big>[</big><var>, deletechars</var><big>]</big>)
<dd>
Return a copy of the string where all characters occurring in the
optional argument <var>deletechars</var> are removed, and the remaining
characters have been mapped through the given translation table, which
must be a string of length 256.
</dl>
<P>
<dl><dt><b><a name='l2h-67'><tt class='method'>upper</tt></a></b> ()
<dd>
Return a copy of the string converted to uppercase.
</dl>
<P>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="typesseq.html" tppabs="http://www.python.org/doc/current/lib/typesseq.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="typesseq.html" tppabs="http://www.python.org/doc/current/lib/typesseq.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="typesseq-strings.html" tppabs="http://www.python.org/doc/current/lib/typesseq-strings.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="typesseq.html" tppabs="http://www.python.org/doc/current/lib/typesseq.html">2.1.5 Sequence Types</A>
<b class="navlabel">Up:</b> <a class="sectref" href="typesseq.html" tppabs="http://www.python.org/doc/current/lib/typesseq.html">2.1.5 Sequence Types</A>
<b class="navlabel">Next:</b> <a class="sectref" href="typesseq-strings.html" tppabs="http://www.python.org/doc/current/lib/typesseq-strings.html">2.1.5.2 String Formatting Operations</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 + -