📄 module-string.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>4.1 string -- Common string operations</title>
<META NAME="description" CONTENT="4.1 string -- Common string operations">
<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="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.html">
<LINK REL="previous" href="strings.html" tppabs="http://www.python.org/doc/current/lib/strings.html">
<LINK REL="up" href="strings.html" tppabs="http://www.python.org/doc/current/lib/strings.html">
<LINK REL="next" href="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.html">
</head>
<body>
<DIV CLASS="navigation"><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>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION006100000000000000000">
4.1 <tt class="module">string</tt> --
Common string operations</A>
</H1>
<P>
<P>
This module defines some constants useful for checking character
classes and some useful string functions. See the module
<tt class='module'><a href="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.html">re</a></tt> for string functions based on regular
expressions.
<P>
The constants defined in this module are are:
<P>
<dl><dt><b><a name='l2h-543'><tt>digits</tt></a></b>
<dd>
The string <code>'0123456789'</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-544'><tt>hexdigits</tt></a></b>
<dd>
The string <code>'0123456789abcdefABCDEF'</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-545'><tt>letters</tt></a></b>
<dd>
The concatenation of the strings <tt class="constant">lowercase</tt> and
<tt class="constant">uppercase</tt> described below.
</dl>
<P>
<dl><dt><b><a name='l2h-546'><tt>lowercase</tt></a></b>
<dd>
A string containing all the characters that are considered lowercase
letters. On most systems this is the string
<code>'abcdefghijklmnopqrstuvwxyz'</code>. Do not change its definition --
the effect on the routines <tt class="function">upper()</tt> and
<tt class="function">swapcase()</tt> is undefined.
</dl>
<P>
<dl><dt><b><a name='l2h-547'><tt>octdigits</tt></a></b>
<dd>
The string <code>'01234567'</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-548'><tt>punctuation</tt></a></b>
<dd>
String of ASCII characters which are considered punctuation
characters in the "<tt class="samp">C</tt>" locale.
</dl>
<P>
<dl><dt><b><a name='l2h-549'><tt>printable</tt></a></b>
<dd>
String of characters which are considered printable. This is a
combination of <tt class="constant">digits</tt>, <tt class="constant">letters</tt>,
<tt class="constant">punctuation</tt>, and <tt class="constant">whitespace</tt>.
</dl>
<P>
<dl><dt><b><a name='l2h-550'><tt>uppercase</tt></a></b>
<dd>
A string containing all the characters that are considered uppercase
letters. On most systems this is the string
<code>'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</code>. Do not change its definition --
the effect on the routines <tt class="function">lower()</tt> and
<tt class="function">swapcase()</tt> is undefined.
</dl>
<P>
<dl><dt><b><a name='l2h-551'><tt>whitespace</tt></a></b>
<dd>
A string containing all characters that are considered whitespace.
On most systems this includes the characters space, tab, linefeed,
return, formfeed, and vertical tab. Do not change its definition --
the effect on the routines <tt class="function">strip()</tt> and <tt class="function">split()</tt>
is undefined.
</dl>
<P>
Many of the functions provided by this module are also defined as
methods of string and Unicode objects; see ``String Methods'' (section
<A href="string-methods.html#string-methods" tppabs="http://www.python.org/doc/current/lib/string-methods.html#string-methods">2.1.5</A>) for more information on those.
The functions defined in this module are:
<P>
<dl><dt><b><a name='l2h-552'><tt class='function'>atof</tt></a></b> (<var>s</var>)
<dd>
<b>Deprecated since release 2.0.</b>
Use the <tt class="function">float()</tt> built-in function.<p>
Convert a string to a floating point number. The string must have
the standard syntax for a floating point literal in Python,
optionally preceded by a sign ("<tt class="samp">+</tt>" or "<tt class="samp">-</tt>"). Note that
this behaves identical to the built-in function
<tt class="function">float()</tt> when passed a string.
<P>
<b>Note:</b> When passing in a string, values for NaN
and Infinity may be returned, depending on the
underlying C library. The specific set of strings accepted which
cause these values to be returned depends entirely on the C library
and is known to vary.
</dl>
<P>
<dl><dt><b><a name='l2h-553'><tt class='function'>atoi</tt></a></b> (<var>s</var><big>[</big><var>, base</var><big>]</big>)
<dd>
<b>Deprecated since release 2.0.</b>
Use the <tt class="function">int()</tt> built-in function.<p>
Convert string <var>s</var> to an integer in the given <var>base</var>. The
string must consist of one or more digits, optionally preceded by a
sign ("<tt class="samp">+</tt>" or "<tt class="samp">-</tt>"). The <var>base</var> defaults to 10. If it
is 0, a default base is chosen depending on the leading characters
of the string (after stripping the sign): "<tt class="samp">0x</tt>" or "<tt class="samp">0X</tt>" means 16, "<tt class="samp">0</tt>" means 8, anything else means 10. If <var>base</var>
is 16, a leading "<tt class="samp">0x</tt>" or "<tt class="samp">0X</tt>" is always accepted, though
not required. This behaves identically to the built-in function
<tt class="function">int()</tt> when passed a string. (Also note: for a more
flexible interpretation of numeric literals, use the built-in
function <tt class="function">eval()</tt>.)
</dl>
<P>
<dl><dt><b><a name='l2h-554'><tt class='function'>atol</tt></a></b> (<var>s</var><big>[</big><var>, base</var><big>]</big>)
<dd>
<b>Deprecated since release 2.0.</b>
Use the <tt class="function">long()</tt> built-in function.<p>
Convert string <var>s</var> to a long integer in the given <var>base</var>.
The string must consist of one or more digits, optionally preceded
by a sign ("<tt class="samp">+</tt>" or "<tt class="samp">-</tt>"). The <var>base</var> argument has the
same meaning as for <tt class="function">atoi()</tt>. A trailing "<tt class="samp">l</tt>" or
"<tt class="samp">L</tt>" is not allowed, except if the base is 0. Note that when
invoked without <var>base</var> or with <var>base</var> set to 10, this
behaves identical to the built-in function
<tt class="function">long()</tt> when passed a string.
</dl>
<P>
<dl><dt><b><a name='l2h-555'><tt class='function'>capitalize</tt></a></b> (<var>word</var>)
<dd>
Capitalize the first character of the argument.
</dl>
<P>
<dl><dt><b><a name='l2h-556'><tt class='function'>capwords</tt></a></b> (<var>s</var>)
<dd>
Split the argument into words using <tt class="function">split()</tt>, capitalize
each word using <tt class="function">capitalize()</tt>, and join the capitalized
words using <tt class="function">join()</tt>. Note that this replaces runs of
whitespace characters by a single space, and removes leading and
trailing whitespace.
</dl>
<P>
<dl><dt><b><a name='l2h-557'><tt class='function'>expandtabs</tt></a></b> (<var>s</var><big>[</big><var>, tabsize</var><big>]</big>)
<dd>
Expand tabs in a string, i.e. replace them by one or more spaces,
depending on the current column and the given tab size. The column
number is reset to zero after each newline occurring in the string.
This doesn't understand other non-printing characters or escape
sequences. The tab size defaults to 8.
</dl>
<P>
<dl><dt><b><a name='l2h-558'><tt class='function'>find</tt></a></b> (<var>s, sub</var><big>[</big><var>, start</var><big>[</big><var>,end</var><big>]</big><big>]</big>)
<dd>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -