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

📄 ch09s07.html

📁 python不错的入门书籍
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css"><!--@import url(stylesheet/text.css);@import url(stylesheet/box.css);--></style><title>简明 Python 教程 / 数据结构 / 更多字符串的内容 </title></head><body><table width="100%"><tr><th colspan="3" align="center"><span class="header">简明 Python 教程</span></th></tr><th colspan="3" align="center">第9章 数据结构</th><tr><th width="20%" align="left"><a href="ch09s06.html">上一页</a></th><th width="60%" align="center"><span class="header2">更多字符串的内容</span></th><th align="right"><a href="ch09s08.html">下一页</a></th></tr></table><hr noshade><h1>更多字符串的内容</h1><p>我们已经在前面详细讨论了字符串。我们还需要知道什么呢?那么,你是否知道字符串也是对象,同样具有方法。这些方法可以完成包括检验一部分字符串和去除空格在内的各种工作。</p><p>你在程序中使用的字符串都是<code>str</code>类的对象。这个类的一些有用的方法会在下面这个例子中说明。如果要了解这些方法的完整列表,请参见<code>help(str)</code>。</p><h2><a name="string">字符串的方法</a></h2><p class="exampletitle"><a name="e97">例9.7 字符串的方法</a></p><p class="filebox"><code class="comment">#!/usr/bin/python<br># Filename: str_methods.py</code><br><br><code>name = </code><code class="cite">'Swaroop' </code><code class="comment"># This is a string object</code><br><br><code class="key">if </code><code>name.startswith(</code><code class="cite">'Swa'</code><code>):</code><br><code class="key">&nbsp;&nbsp;&nbsp;&nbsp;print </code><code class="cite">'Yes, the string starts with "Swa"'</code><br><br><code class="key">if </code><code class="cite">'a' </code><code class="key">in </code><code>name:</code><br><code class="key">&nbsp;&nbsp;&nbsp;&nbsp;print </code><code class="cite">'Yes, it contains the string "a"'</code><br><br><code class="key">if </code><code>name.find(</code><code class="cite">'war'</code><code>) != </code><code class="cite">-1</code><code>:</code><br><code class="key">&nbsp;&nbsp;&nbsp;&nbsp;print </code><code class="cite">'Yes, it contains the string "war"'</code><br><br><code>delimiter = </code><code class="cite">'_*_'</code><br><code>mylist = [</code><code class="cite">'Brazil'</code><code>, </code><code class="cite">'Russia'</code><code>, </code><code class="cite">'India'</code><code>, </code><code class="cite">'China'</code><code>]</code><br><code class="key">print </code><code>delimiter.join(mylist)</code></p><p>(源文件:<a href="code/str_methods.py">code/str_methods.py</a>)</p><h2>输出</h2><p class="codebox"><code>$ python str_methods.py<br>Yes, the string starts with "Swa"<br>Yes, it contains the string "a"<br>Yes, it contains the string "war"<br>Brazil_*_Russia_*_India_*_China</code></p><h2>它如何工作</h2><p>这里,我们看到使用了许多字符串方法。<code>startwith</code>方法是用来测试字符串是否以给定字符串开始。<code>in</code>操作符用来检验一个给定字符串是否为另一个字符串的一部分。</p><p><code>find</code>方法用来找出给定字符串在另一个字符串中的位置,或者返回-1以表示找不到子字符串。<code>str</code>类也有以一个作为分隔符的字符串<code>join</code>序列的项目的整洁的方法,它返回一个生成的大字符串。</p><hr noshade><table width="100%"><tr><th width="20%" align="left"><a href="ch09s06.html">上一页</a></th><th width="60%" align="center"><a href="ch09.html">上一级</a></th><th width="20%" align="right"><a href="ch09s08.html">下一页</a></th></tr><tr><th width="20%" align="left">参考</th><th width="60%" align="center"><a href="index.html">首页</a></th><th align="right">概括</th></tr></table></body></html>

⌨️ 快捷键说明

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