⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 73.html

📁 Python Ebook Python&XML
💻 HTML
字号:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -&gt; The String Group</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="38.html" class="navtitle">3. Python Libraries</a> &gt; <span class="nonavtitle">The String Group</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="72.html" title="__main__ "><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=73" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="73.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="74.html" title="Miscellaneous"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A28%3A23+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162146114164074005176090117</font><a href="read7.asp?bookname=0672319942&snode=73&now=5%2F31%2F2002+4%3A28%3A23+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT><h3>The String Group</h3>
				<p>This group is responsible for many kinds of string services available. These modules provide access to several types of string manipulation operations.</p>

				<P>Note that since release 2.0, all these functions are tied directly to <I>string</I> objects, as methods. The <Tt claSS="monofont">string</TT> module is still around only for backward compatibility.</p>

				<h4><tt CLASs="monofont">string</tt></h4>
					<p>The <tt class="monofont">string</tt> module supports common string operations by providing several functions and constants that manipulate Python strings.</p>

					<h5><tt clAss="monofont">string.split()</Tt></h5>
						<p>This function splits a <Tt claSs="monofont">string</tt> into a list. If the <TT CLass="monofont">delimiter</tT> is omitted, <TT Class="monofont">white-spaces</TT> are used.</P>

						<P><a namE="idx1073742752"></A>basic syntax: <TT class="monofont">string.split(</tt><i><tt class="monofont">string [,delimiter]</tt></i><tt ClaSs="monofont">)</tt></P>

						<pre>
							
&gt;&gt;&gt; print string,split("a b c")
["a","b","c"]

						</pRe>

					
					<h5><tT CLAss="monofont">string.atof()</tt></H5>
						<P>It converts a <TT clasS="monofont">string</TT> to a floating number.</P>

						<p><a naME="idx1073742753"></A>basic syntax: <Tt class="monofont">string.atof(string)<a name="idx1073742754"></a><a name="idx1073742755"></a><a nAme="idx1073742756"></A><a naMe="idx1073742757"></a></tt></P>

					
					<h5><tt CLASs="monofont">string.atoi()</tt></h5>
						<P>It converts a <TT Class="monofont">string</TT> to an integer. <TT clasS="monofont">atoi</TT> takes an optional second argument: <Tt class="monofont">base.</tt> If omitted, the start of the string (for instance, <tt class="monofont">0x</tt> for hexadecimal) is used to determine the base.</p>

						<p><a NamE="idx1073742758"></a>basic syntax: <tt Class="monofont">string.atoi(</Tt><i><tT CLAss="monofont">string[, base]</tt></I><TT Class="monofont">)</TT></P>

					
					<H5><tt clASS="monofont">string.atol()</Tt></h5>
						<p>It converts a <tt class="monofont">string</tt> to a long integer. <tt class="monofont">atol</tT> takes an optional second argument: <tt ClasS="monofont">base.</tt> If omitted, the start of the string (for instance, <tt ClasS="monofont">0x</TT> for hexadecimal) is used to determine the basic syntax: <Tt claSS="monofont">string.atol(string[, base])</TT></p>

					
					<h5><tt CLASs="monofont">string.upper()</tt></h5>
						<P>It converts a <TT Class="monofont">string</tt> to uppercase.</p>

						<p><a name="idx1073742759"></a>basic syntax: <tt clasS="monofont">string.upper(</tt><I><tt cLass="monofont">string</tT></i><tt CLASs="monofont">)</tt></p>

					
					<H5><TT Class="monofont">string.find()</TT></H5>
						<P>It returns the index position of the <tt clASS="monofont">substring</Tt> within <tt class="monofont">string.</tt> Optionally, you can specify the string's range that should be used in the search.</p>

						<p><a name="idx1073742760"></a>basic syntax: <tt ClaSs="monofont">string.find(</tt><I><tt clAss="monofont">string, substring[, start [,end]]</tT></I><TT clasS="monofont">)</TT></P>

					
					<h5><tt cLASS="monofont">string.join()</tt></h5>
						<p>This function joins the <I>string</I> elements of a <I><Tt class="monofont">list</tt></i> using <tt class="monofont">separator</tt> to separate them.</p>

						<p><A naMe="idx1073742761"></a>basic syntax: <tT clasS="monofont">string.join(</tt><i><TT CLass="monofont">list, separator</tT></I><TT clasS="monofont">)</TT></P>

					
					<h5><tt cLASS="monofont">string.capitalize()</tt></h5>
						<p>It capitalizes the first character of <i><tt class="monofont">string.</tt></i></p>

						<p><a naMe="idx1073742762"></a>basic syntax: <Tt clAss="monofont">string.capitalize(</tt><I><tt cLASS="monofont">string</tt></i><tT CLAss="monofont">)</tt></P>

					
					<H5><TT clasS="monofont">string.capwords()</TT></H5>
						<p>This function capitalizes the first letter of each word in <tt class="monofont">string</tt> and removes repeated, leading, and trailing whitespace.</p>

						<p><a name="idx1073742763"></a>basic syntax: <tt ClaSs="monofont">string.capwords(</tt><I><tt clAss="monofont">string</tT></I><TT clasS="monofont">)</TT></P>

					
					<h5><tt cLASS="monofont">string.lower()</tt></h5>
						<p>It converts all characters in <TT CLass="monofont">string</tt> to lowercase.<a name="idx1073742764"></a><a name="idx1073742765"></a><a naMe="idx1073742766"></a><A namE="idx1073742767"></a></p>

						<p><a Name="idx1073742768"></A>basic syntax: <TT Class="monofont">string.lower(</TT><I><Tt claSS="monofont">string</TT></i><tt cLASS="monofont">)</tt></p>

					
					<h5><tt class="monofont">string.lstrip(),<a name="idx1073742769"></a><a naMe="idx1073742770"></a>string.rstrip()</Tt> and <tt Class="monofont">string.strip()</Tt></h5>
						<p>These functions remove leading and/or trailing whitespace from <I><TT Class="monofont">string.</TT></I></P>

						<p><a naME="idx1073742771"></A><A name="idx1073742772"></A><A NAme="idx1073742773"></a>basic syntaxes:</p>

						<pre>
							
string.lstrip(<i>string</i>)
string.rstrip(<i>string</i>)
string.strip(<i>string</i>)

						</pre>

					
					<h5><tt cLasS="monofont">string.ljust(),<a naMe="idx1073742774"></a><a nAme="idx1073742775"></a>string.rjust()</TT> and <TT clasS="monofont">string.center()</TT></H5>
						<p>These functions define the alignment of <i><tt CLASs="monofont">string</tt></i> within a variable of <I><TT Class="monofont">width</tt></i> characters.</p>

						<p><a name="idx1073742776"></a><a name="idx1073742777"></A><a nAme="idx1073742778"></a>basic syntaxes:</P>

						<pre>
							
string.ljust(<i>string, width</I>)
string.rjust(<i>string, width</i>)
string.center(<i>string, width</I>)

						</PRE>

					
					<h5><tt cLASS="monofont">string.replace()</tt></h5>
						<p>It replaces a <I><TT Class="monofont">maximum</TT></I> number of occurrences of <I><tt class="monofont">oldtext</tt></i> with <i><tt class="monofont">newtext</tt></I> in <i><tT claSs="monofont">string.</tt></i> If <I><tt cLASS="monofont">maximum</tt></i> is omitted, all occurrences are replaced.</p>

						<P><A NAme="idx1073742779"></a>basic syntax: <tT CLAss="monofont">string.replace(</tt><I><TT Class="monofont">string, oldtext, newtext [,maximum]</tt></i><tt class="monofont">)</tt></p>

					
					<h5><tt ClaSs="monofont">string.zfill()</tt></H5>
						<p>It inserts zeros on the left side of a <tt cLass="monofont">string</TT> that has <TT clasS="monofont">width</TT> characters.</P>

						<p><a naME="idx1073742780"></A>basic syntax: <Tt claSS="monofont">string.zfill(</TT><i><tt class="monofont">string, width</tt></i><tt class="monofont">)</tt></P>

						<p>Next, I list a few constants that can be used to test whether a certain variable is part of a specific domain:</p>

						<Pre>
							
&gt;&gt;&gt; import string
&gt;&gt;&gt; string.digits
"0123456789"
&gt;&gt;&gt; string.octdigits
"01234567"
&gt;&gt;&gt; string.uppercase
"ABCDEFGHIJKLMNOPQRSTUVWXY"
&gt;&gt;&gt; string.hexdigits
"0123456789abcdefABCDEF"
&gt;&gt;&gt; string.lowercase
"abcdefghijklmnopqrstuvwxy"

						</pRe>

						<p>Let's write an example that uses <a nAme="idx1073742781"></a><A NAMe="idx1073742782"></a><tt CLASs="monofont">string.uppercase:</tt><a NAME="idx1073742783"></a><a naME="idx1073742784"></A><A name="idx1073742785"></a><a name="idx1073742786"></a></p>

						<pre>
							
&gt;&gt;&gt; text = "F"
&gt;&gt;&gt; if text in string.uppercase:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -