Python全栈之路系列之字符串数据类型

ou378346 7年前
   <h2>字符串(str)</h2>    <p>字符串类型是python的序列类型,他的本质就是字符序列,而且python的字符串类型是不可以改变的,你无法将原字符串进行修改,但是可以将字符串的一部分复制到新的字符串中,来达到相同的修改效果。</p>    <p>创建字符串类型可以使用单引号或者双引号又或者三引号来创建,实例如下:</p>    <p>单引号</p>    <pre>  <code class="language-python">>>> string = 'ansheng'  # type是查看一个变量的数据类型  >>> type(string)  <class 'str'></code></pre>    <p>双引号</p>    <pre>  <code class="language-python">>>> string = "ansheng"  # type是查看一个变量的数据类型  >>> type(string)   <class 'str'></code></pre>    <p>三引号</p>    <pre>  <code class="language-python">>>> string = """ansheng"""  >>> type(string)  <class 'str'></code></pre>    <p>还可以指定类型</p>    <pre>  <code class="language-python">>>> var=str("string")  >>> var  'string'  >>> type(var)  <class 'str'></code></pre>    <h3>字符串方法</h3>    <p>每个类的方法其实都是很多的,无论我们在学习的过程中个还是工作的时候,常用的其实没有多少,所以我们没必要去可以得记那么多,有些方法我们只需要对其有个印象就ok了,忘了的时候可以google一下。</p>    <p>首字母变大写</p>    <p>capitalize(self):</p>    <pre>  <code class="language-python">>>> name="ansheng"  >>> name.capitalize()  'Ansheng'</code></pre>    <p>内容居中,width:字符串的总宽度;fillchar:填充字符,默认填充字符为空格。</p>    <p>center(self, width, fillchar=None):</p>    <pre>  <code class="language-python"># 定义一个字符串变量,名为"string",内容为"hello word"  >>> string="hello word"  # 输出这个字符串的长度,用len(value_name)  >>> len(string)  10  # 字符串的总宽度为10,填充的字符为"*"  >>> string.center(10,"*")  'hello word'  # 如果设置字符串的总产都为11,那么减去字符串长度10还剩下一个位置,这个位置就会被*所占用  >>> string.center(11,"*")  '*hello word'  # 是从左到右开始填充  >>> string.center(12,"*")  '*hello word*'</code></pre>    <p>统计字符串里某个字符出现的次数,可选参数为在字符串搜索的开始与结束位置。</p>    <p>count(self, sub, start=None, end=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>sub</td>       <td>搜索的子字符串;</td>      </tr>      <tr>       <td>start</td>       <td>字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0;</td>      </tr>      <tr>       <td>end</td>       <td>字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置;</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python">>>> string="hello word"  # 默认搜索出来的"l"是出现过两次的  >>> string.count("l")  2  # 如果指定从第三个位置开始搜索,搜索到第六个位置,"l"则出现过一次  >>> string.count("l",3,6)  1</code></pre>    <p>解码</p>    <p>decode(self, encoding=None, errors=None):</p>    <pre>  <code class="language-python"># 定义一个变量内容为中文  temp = "中文"  # 把变量的字符集转化为UTF-8  temp_unicode = temp.decode("utf-8")</code></pre>    <p>编码,针对unicode</p>    <p>encode(self, encoding=None, errors=None):</p>    <pre>  <code class="language-python"># 定义一个变量内容为中文,字符集为UTF-8  temp = u"中文"  # 编码,需要指定要转换成什么编码  temp_gbk = temp_unicode.encode("gbk")</code></pre>    <p>于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。</p>    <p>endswith(self, suffix, start=None, end=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>suffix</td>       <td>后缀,可能是一个字符串,或者也可能是寻找后缀的tuple</td>      </tr>      <tr>       <td>start</td>       <td>开始,切片从这里开始</td>      </tr>      <tr>       <td>end</td>       <td>结束,片到此为止</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python">>>> string="hello word"  # 判断字符串中是否已"d"结尾,如果是则返回"True"  >>> string.endswith("d")  True  # 判断字符串中是否已"t"结尾,不是则返回"False"  >>> string.endswith("t")  False  # 制定搜索的位置,实则就是从字符串位置1到7来进行判断,如果第七个位置是"d",则返回True,否则返回False  >>> string.endswith("d",1,7)  False</code></pre>    <p>把字符串中的tab符号('t')转为空格,tab符号('t')默认的空格数是8。</p>    <p>expandtabs(self, tabsize=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>tabsize</td>       <td>指定转换字符串中的 tab 符号('t')转为空格的字符数</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python">>>> string="hello       word"  # 输出变量"string"内容的时候会发现中间有一个"\t",这个其实就是一个`tab`键  >>> string  'hello\tword'  # 把`tab`键换成一个空格  >>> string.expandtabs(1)  'hello word'  # 把`tab`键换成十个空格  >>> string.expandtabs(10)  'hello     word'</code></pre>    <p>检测字符串中是否包含子字符串str,如果指定beg(开始)和end(结束)范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。</p>    <p>find(self, sub, start=None, end=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>str</td>       <td>指定检索的字符串</td>      </tr>      <tr>       <td>beg</td>       <td>开始索引,默认为0</td>      </tr>      <tr>       <td>end</td>       <td>结束索引,默认为字符串的长度</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python">>>> string="hello word"  # 返回`o`在当前字符串中的位置,如果找到第一个`o`之后就不会再继续往下面寻找了  >>> string.find("o")  4  # 从第五个位置开始搜索,返回`o`所在的位置  >>> string.find("o",5)  7</code></pre>    <p>字符串格式,后续文章会提到。</p>    <p>format( <em>args,</em> *kwargs):</p>    <p>检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。</p>    <p>index(self, sub, start=None, end=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>str</td>       <td>指定检索的字符串</td>      </tr>      <tr>       <td>beg</td>       <td>开始索引,默认为0</td>      </tr>      <tr>       <td>end</td>       <td>结束索引,默认为字符串的长度</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python">>>> string="hello word"  # 返回字符串所在的位置  >>> string.index("o")  4  # 如果查找一个不存在的字符串那么就会报错  >>> string.index("a")  Traceback (most recent call last):    File "<stdin>", line 1, in <module>  ValueError: substring not found</code></pre>    <p>法检测字符串是否由字母和数字组成,如果string至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False</p>    <p>isalnum(self):</p>    <pre>  <code class="language-python">>>> string="hes2323"  # 如果存在数字或字母就返回`True`,否则返回`False`  >>> string.isalnum()  True  # 中间有空格返回的就是False了  >>> string="hello word"  >>> string.isalnum()  False</code></pre>    <p>检测字符串是否只由字母组成。</p>    <p>isalpha(self):</p>    <pre>  <code class="language-python"># 如果全部都是字母就返回`True`  >>> string="helloword"  >>> string.isalpha()  True  # 否则就返回False  >>> string="hes2323"  >>> string.isalpha()  False</code></pre>    <p>检测字符串是否只由数字组成</p>    <p>isdigit(self):</p>    <pre>  <code class="language-python"># 如果变量里面都是数字就返回`True`,否则就返回`False`  >>> string="hes2323"  >>> string.isdigit()  False  >>> string="2323"  >>> string.isdigit()  True</code></pre>    <p>检测字符串是否由小写字母组成</p>    <p>islower(self):</p>    <pre>  <code class="language-python"># 如果变量内容全部都是小写字母就返回`True`,否则就返回`False`  >>> string="hesasdasd"  >>> string.islower()  True  >>> string="HelloWord"  >>> string.islower()  False</code></pre>    <p>检测字符串是否只由空格组成</p>    <p>isspace(self):</p>    <pre>  <code class="language-python"># 如果变量内容由空格来组成,那么就返回`True`否则就返回`False`  >>> string=" "  >>> string.isspace()  True  >>> string="a"  >>> string.isspace()  False</code></pre>    <p>检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。</p>    <p>istitle(self):</p>    <pre>  <code class="language-python"># 如果变量的内容首字母是大写并且其他字母为小写,那么就返回`True`,否则会返回`False`  >>> string="Hello Word"  >>> string.istitle()  True  >>> string="Hello word"  >>> string.istitle()  False</code></pre>    <p>检测字符串中所有的字母是否都为大写。</p>    <p>isupper(self):</p>    <pre>  <code class="language-python"># 如果变量值中所有的字母都是大写就返回`True`,否则就返回`False`  >>> string="hello word"  >>> string.isupper()  False  >>> string="HELLO WORD"  >>> string.isupper()  True</code></pre>    <p>将序列中的元素以指定的字符连接生成一个新的字符串。</p>    <p>join(self, iterable):</p>    <pre>  <code class="language-python">>>> string=("a","b","c")  >>> '-'.join(string)  'a-b-c'</code></pre>    <p>返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。</p>    <p>ljust(self, width, fillchar=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>width</td>       <td>指定字符串长度</td>      </tr>      <tr>       <td>fillchar</td>       <td>填充字符,默认为空格</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python">>>> string="helo word"  >>> len(string)  9  # 定义的长度减去字符串的长度,剩下的就开始填充  >>> string.ljust(15,'*')  'helo word******'</code></pre>    <p>转换字符串中所有大写字符为小写。</p>    <p>lower(self):</p>    <pre>  <code class="language-python"># 把变量里的大写全部转换成小写  >>> string="Hello WORD"  >>> string.lower()  'hello word'</code></pre>    <p>截掉字符串左边的空格或指定字符</p>    <p>lstrip(self, chars=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>chars</td>       <td>指定截取的字符</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python"># 从左侧开始删除匹配的字符串  >>> string="hello word"  >>> string.lstrip("hello ")  'word'</code></pre>    <p>用来根据指定的分隔符将字符串进行分割,如果字符串包含指定的分隔符,则返回一个3元的tuple,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。</p>    <p>partition(self, sep):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>str</td>       <td>指定的分隔符</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python"># 返回的是一个元组类型  >>> string="www.ansheng.me"  >>> string.partition("ansheng")  ('www.', 'ansheng', '.me')</code></pre>    <p>把字符串中的 old(旧字符串)替换成new(新字符串),如果指定第三个参数max,则替换不超过max次</p>    <p>replace(self, old, new, count=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>old</td>       <td>将被替换的子字符串</td>      </tr>      <tr>       <td>new</td>       <td>新字符串,用于替换old子字符串</td>      </tr>      <tr>       <td>count</td>       <td>可选字符串, 替换不超过count次</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python">>>> string="www.ansheng.me"  # 把就字符串`www.`换成新字符串`https://`  >>> string.replace("www.","https://")  'https://blog.ansheng.me'  # 就字符串`w`换成新字符串`a`只替换`2`次  >>> string.replace("w","a",2)  'aaw.ansheng.me'</code></pre>    <p>返回字符串最后一次出现的位置,如果没有匹配项则返回-1。</p>    <p>rfind(self, sub, start=None, end=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>str</td>       <td>查找的字符串</td>      </tr>      <tr>       <td>beg</td>       <td>开始查找的位置,默认为0</td>      </tr>      <tr>       <td>end</td>       <td>结束查找位置,默认为字符串的长度</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python">>>> string="hello word"  # rfind其实就是反向查找  >>> string.rfind("o")  7  # 指定查找的范围  >>> string.rfind("o",0,6)  4</code></pre>    <p>返回子字符串str在字符串中最后出现的位置,如果没有匹配的字符串会报异常,你可以指定可选参数 [beg:end] 设置查找的区间。</p>    <p>rindex(self, sub, start=None, end=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>str</td>       <td>查找的字符串</td>      </tr>      <tr>       <td>beg</td>       <td>开始查找的位置,默认为0</td>      </tr>      <tr>       <td>end</td>       <td>结束查找位置,默认为字符串的长度</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python">>>> string="hello word"  # 反向查找索引  >>> string.rindex("o")  7  # 如果没有查找到就报错  >>> string.rindex("a")  Traceback (most recent call last):    File "<stdin>", line 1, in <module>  ValueError: substring not found</code></pre>    <p>返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。如果指定的长度小于字符串的长度则返回原字符串。</p>    <p>rjust(self, width, fillchar=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>width</td>       <td>指定填充指定字符后中字符串的总长度</td>      </tr>      <tr>       <td>fillchar</td>       <td>填充的字符,默认为空格</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python">>>> string="hello word"  >>> len(string)  10  >>> string.rjust(10,"*")  'hello word'  >>> string.rjust(12,"*")  '**hello word'</code></pre>    <p>从右到左通过指定分隔符对字符串进行切片,如果参数num有指定值,则仅分隔num个子字符串</p>    <p>rsplit(self, sep=None, maxsplit=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>str</td>       <td>隔符,默认为空格</td>      </tr>      <tr>       <td>num</td>       <td>分割次数</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python">>>> string="www.ansheng.me"  >>> string.rsplit(".",1)  ['www.ansheng', 'me']  >>> string.rsplit(".",2)  ['www', 'ansheng', 'me']</code></pre>    <p>删除string字符串末尾的指定字符(默认为空格).</p>    <p>rstrip(self, chars=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>chars</td>       <td>指定删除的字符</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python"># 从尾部开始匹配删除  >>> string="hello word"  >>> string.rstrip("d")  'hello wor'</code></pre>    <p>从左到右通过指定分隔符对字符串进行切片,如果参数num有指定值,则仅分隔num个子字符串</p>    <p>split(self, sep=None, maxsplit=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>str</td>       <td>分隔符,默认为空格</td>      </tr>      <tr>       <td>num</td>       <td>分割次数</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python">>>> string="www.ansheng.me"  # 指定切一次,以`.`来分割  >>> string.split(".",1)  ['www', 'ansheng.me']  # 指定切二次,以`.`来分割  >>> string.split(".",2)  ['www', 'ansheng', 'me']</code></pre>    <p>按照行分隔,返回一个包含各行作为元素的列表,如果num指定则仅切片num个行.</p>    <p>splitlines(self, keepends=False):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>num</td>       <td>分割行的次数</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python"># 定义一个有换行的变量,`\n`可以划行  >>> string="www\nansheng\nme"  # 输出内容  >>> print(string)  www  ansheng  me  # 把有行的转换成一个列表  >>> string.splitlines(1)  ['www\n', 'ansheng\n', 'me']</code></pre>    <p>检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。</p>    <p>startswith(self, prefix, start=None, end=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>str</td>       <td>检测的字符串</td>      </tr>      <tr>       <td>strbeg</td>       <td>可选参数用于设置字符串检测的起始位置</td>      </tr>      <tr>       <td>strend</td>       <td>可选参数用于设置字符串检测的结束位置</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python">>>> string="www.ansheng.me"  >>> string.startswith("www")  True  >>> string.startswith("www",3)  False</code></pre>    <p>移除字符串头尾指定的字符(默认为空格)</p>    <p>strip(self, chars=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>chars</td>       <td>移除字符串头尾指定的字符</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python">>>> string=" www.ansheng.me "  >>> string  ' www.ansheng.me '  # 删除空格  >>> string.strip()  'www.ansheng.me'  >>> string="_www.ansheng.me_"  # 指定要把左右两边的"_"删除掉  >>> string.strip("_")  'www.ansheng.me'</code></pre>    <p>用于对字符串的大小写字母进行转换,大写变小写,小写变大写</p>    <p>swapcase(self):</p>    <pre>  <code class="language-python">>>> string="hello WORD"  >>> string.swapcase()  'HELLO word'</code></pre>    <p>返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写。</p>    <p>title(self):</p>    <pre>  <code class="language-python">>>> string="hello word"  >>> string.title()  'Hello Word'</code></pre>    <p>根据参数table给出的表(包含 256 个字符)转换字符串的字符, 要过滤掉的字符放到 del 参数中。</p>    <p>translate(self, table, deletechars=None):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>table</td>       <td>翻译表,翻译表是通过maketrans方法转换而来</td>      </tr>      <tr>       <td>deletechars</td>       <td>字符串中要过滤的字符列表</td>      </tr>     </tbody>    </table>    <p>将字符串中的小写字母转为大写字母</p>    <p>upper(self):</p>    <pre>  <code class="language-python">>>> string="hello word"  >>> string.upper()  'HELLO WORD'</code></pre>    <p>返回指定长度的字符串,原字符串右对齐,前面填充0</p>    <p>zfill(self, width):</p>    <table>     <thead>      <tr>       <th>参数</th>       <th>描述</th>      </tr>     </thead>     <tbody>      <tr>       <td>width</td>       <td>指定字符串的长度。原字符串右对齐,前面填充0</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python">>>> string="hello word"  >>> string.zfill(10)  'hello word'  >>> string.zfill(20)  '0000000000hello word'</code></pre>    <p>去除值得两端空格</p>    <pre>  <code class="language-python">>>> var=" ansheng "  >>> var  ' ansheng '  >>> var.strip()  'ansheng'</code></pre>    <h2>str类型和bytes类型转换</h2>    <p>以UTF-8编码的时候,一个汉字是三个字节,一个字节是八位</p>    <p>3.5.x实例</p>    <p>代码如下:</p>    <pre>  <code class="language-python">#!/usr/bin/env python  # _*_ coding:utf-8 _*_    var = "中文"  for n in var:      print(n)    print("================")    var2 = "zhongwen"  for n in var2:      print(n)</code></pre>    <p>执行结果:</p>    <pre>  <code class="language-python">C:\Python35\python.exe F:/Python_code/sublime/Day03/str.py  中  文  ================  z  h  o  n  g  w  e  n</code></pre>    <p>2.7.x实例</p>    <p>代码如下:</p>    <pre>  <code class="language-python">#!/usr/bin/env python  # _*_ coding:utf-8 _*_    var = "中文"  for n in var:      print(n)    print("================")    var2 = "zhongwen"  for n in var2:      print(n)</code></pre>    <p>执行结果</p>    <pre>  <code class="language-python">C:\Python27\python.exe F:/Python_code/sublime/Day03/str.py  �  �  �  �  �  �  ================  z  h  o  n  g  w  e  n</code></pre>    <p>通过上面的实例可以知道, Python3.5.x 在输出中文或者英文的时候是按照一个字符一个字符来输出的,但是在 Python2.7.x 就不这样了, Python2.7.x 是按照字节来进行输出的,可以看到在输出中文的时候是乱码的,而且还输出了六次,因为在UTF-8编码的情况下一个汉字是等于三个字节的,所以输出了六个乱码的字符。</p>    <p>在Python3.5.x里面是既可以输出汉字,也可以把输出字节的,利用bytes这个方法,bytes可以将字符串转换为字节</p>    <pre>  <code class="language-python">var="中文"  for n in var:      print(n)      bytes_list = bytes(n, encoding='utf-8')      # 十六进制输出      print(bytes_list)      for x in bytes_list:          # 十进制,bin(x)二进制          print(x,bin(x))</code></pre>    <p>输出的结果</p>    <pre>  <code class="language-python"># 字符串  中  # 十六进制  b'\xe4\xb8\xad'  # 228=十进制,0b11100100=二进制  228 0b11100100  184 0b10111000  173 0b10101101  文  b'\xe6\x96\x87'  230 0b11100110  150 0b10010110  135 0b10000111</code></pre>    <p>b代表十六进制,xe4这样的是一个十六进制的字节</p>    <h2>其他知识点</h2>    <h3>索引</h3>    <p>索引是指某个值在列表或别的数据类型中的一个位置</p>    <p>定义一个列表,查看列表中 Linux 值对应在列表中的位置</p>    <pre>  <code class="language-python">>>> list_os = ["Windows","Linux","Mac","Unix"]  >>> list_os.index("Linux")  1  >>> list_os[1]  'Linux'</code></pre>    <h3>使用 \ 转义</h3>    <p>Python允许你对某些字符进行转义,以此来实现一些难以单纯用字符描述的效果</p>    <pre>  <code class="language-python"># 常用的内容也转义也就是`\n`和`\t`了,`\n`是用来换行的,`\t`是用来代替一个`tab`键  >>> string="My \n Name  \t is"  >>> print(string)  My   Name    is</code></pre>    <h3>使用 + 拼接</h3>    <p>你可以使用 + 号将多个字符串或字符串变量拼接起来</p>    <pre>  <code class="language-python">>>> a="my "  >>> b="name "  >>> c="is "  >>> d="ansheng"  >>> a+b+c+d  'my name is ansheng'</code></pre>    <h3>切片</h3>    <p>切片操作符是序列名后跟一个方括号,方括号中有一对可选的数字,并用冒号分割。注意这与你使用的索引操作符十分相似。记住数是可选的,而冒号是必须的,切片操作符中的第一个数表示切片开始的位置,第二个数表示切片到哪里结束,第三个数表示切片间隔数。如果不指定第一个数,Python就从序列首开始。如果没有指定第二个数,则Python会停止在序列尾。注意,返回的序列从开始位置开始 ,刚好在结束位置之前结束。即开始位置是包含在序列切片中的,而结束位置被排斥在切片外。</p>    <pre>  <code class="language-python">>>> os="Linux"  >>> os  'Linux'  >>> os[0:2]  'Li'  >>> os[0:4:2]  'Ln'</code></pre>    <p>更多实例如下</p>    <table>     <thead>      <tr>       <th>切片符</th>       <th>说明</th>      </tr>     </thead>     <tbody>      <tr>       <td>[:]</td>       <td>提取从开头到结尾的整个字符串</td>      </tr>      <tr>       <td>[start:]</td>       <td>从start到结尾的字符串</td>      </tr>      <tr>       <td>[:end]</td>       <td>从开头提取到end - 1</td>      </tr>      <tr>       <td>[start:end]</td>       <td>从start提取到end - 1</td>      </tr>      <tr>       <td>[startsetp]</td>       <td>从start提取到end-1,每setp个字符提取一个</td>      </tr>     </tbody>    </table>    <p>索引和切片同时适用于字符串、列表与元组</p>    <ol>     <li> <p>索引通常用于查找某一个字符串或值</p> </li>     <li> <p>切片通常用于查找某一个范围内的字符串或值</p> </li>    </ol>    <p>实例:</p>    <pre>  <code class="language-python"># 定义一个列表,列表内有三个元素  >>> var=["Linux","Win","Unix"]  # 通过索引取到了一个值  >>> var[0]  'Linux'  # 通过切片取到了多个值  >>> var[0:2]  ['Linux', 'Win']  >>> var[1:3]  ['Win', 'Unix']</code></pre>    <p> </p>    <p> </p>    <p>来自:https://segmentfault.com/a/1190000008078178</p>    <p> </p>