教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 详解Python3字符串

详解Python3字符串

发布时间:2021-12-16   编辑:jiaochengji.com
教程集为您提供详解Python3字符串等资源,欢迎您收藏本站,我们将为您提供最新的详解Python3字符串资源

Python 字符串

除了数字,Python也能操作字符串。字符串有几种表达方式,可以使用单引号或双引号括起来:

<pre class="brush:html;toolbar:false">>>> 'spam eggs' 'spam eggs' >>> 'doesn\'t' "doesn't" >>> "doesn't" "doesn't" >>> '"Yes," he said.' '"Yes," he said.' >>> "\"Yes,\" he said." '"Yes," he said.' >>> '"Isn\'t," she said.' '"Isn\'t," she said.'</pre>

Python中使用反斜杠转义引号和其它特殊字符来准确地表示。

如果字符串包含有单引号但不含双引号,则字符串会用双引号括起来,否则用单引号括起来。对于这样的输入字符串,print() 函数会产生更易读的输出。

跨行的字面字符串可用以下几种方法表示。使用续行符,即在每行最后一个字符后使用反斜线来说明下一行是上一行逻辑上的延续:

以下使用 \n 来添加新行:

<pre class="brush:html;toolbar:false">>>> '"Isn\'t," she said.' '"Isn\'t," she said.' >>> print('"Isn\'t," she said.') "Isn't," she said. >>> s = 'First line.\nSecond line.'  # \n 意味着新行 >>> s  # 不使用 print(), \n 包含在输出中 'First line.\nSecond line.' >>> print(s)  # 使用 print(), \n 输出一个新行 First line. Second line.</pre>

以下使用 反斜线(\) 来续行:

<pre class="brush:html;toolbar:false">hello = "This is a rather long string containing\n\ several lines of text just as you would do in C.\n\     Note that whitespace at the beginning of the line is\  significant." print(hello)</pre>

注意,其中的换行符仍然要使用 \n 表示——反斜杠后的换行符被丢弃了。以上例子将如下输出:

<pre class="brush:html;toolbar:false">This is a rather long string containing several lines of text just as you would do in C.     Note that whitespace at the beginning of the line is significant.</pre>

或者,字符串可以被 """ (三个双引号)或者 ''' (三个单引号)括起来。使用三引号时,换行符不需要转义,它们会包含在字符串中。以下的例子使用了一个转义符,避免在最开始产生一个不需要的空行。

<pre class="brush:html;toolbar:false">print("""\ Usage: thingy [OPTIONS]      -h                        Display this usage message      -H hostname               Hostname to connect to """)</pre>

其输出如下:

<pre class="brush:html;toolbar:false">Usage: thingy [OPTIONS]      -h                        Display this usage message      -H hostname               Hostname to connect to</pre>

如果我们使用"原始"字符串,那么 \n 不会被转换成换行,行末的的反斜杠,以及源码中的换行符,都将作为数据包含在字符串内。例如:

<pre class="brush:html;toolbar:false">hello = r"This is a rather long string containing\n\ several lines of text much as you would do in C." print(hello)</pre>

将会输出

<pre class="brush:html;toolbar:false">This is a rather long string containing\n\ several lines of text much as you would do in C.</pre>

字符串可以使用 运算符串连接在一起,或者用 * 运算符重复:

<pre class="brush:html;toolbar:false">>>> word = 'Help'   'A' >>> word 'HelpA' >>> '<'   word*5   '>' ''</pre>

两个紧邻的字面字符串将自动被串连;上例的第一行也可以写成 word = 'Help' 'A' ;这样的操作只在两个字面值间有效,不能随意用于字符串表达式中:

<pre class="brush:html;toolbar:false">>>> 'str' 'ing'                   #  <- string="">>> 'str'.strip()   'ing'   #  <- string="">>> 'str'.strip() 'ing'     #  <-  这样操作错误   File "", line 1, in ?     'str'.strip() 'ing'                       ^ SyntaxError: invalid syntax</pre>

字符串可以被索引;就像 C 语言一样,字符串的第一个字符的索引为 0。没有单独的字符类型;一个字符就是长度为一的字符串。就像Icon编程语言一样,子字符串可以使用分切符来指定:用冒号分隔的两个索引。

<pre class="brush:html;toolbar:false">>>> word[4] 'A' >>> word[0:2] 'Hl' >>> word[2:4] 'ep'</pre>

默认的分切索引很有用:默认的第一个索引为零,第二个索引默认为字符串可以被分切的长度。

<pre class="brush:html;toolbar:false">>>> word[:2]    # 前两个字符 'He' >>> word[2:]    # 除了前两个字符之外,其后的所有字符 'lpA'</pre>

不同于C字符串的是,Python字符串不能被改变。向一个索引位置赋值会导致错误:

<pre class="brush:html;toolbar:false">>>> word[0] = 'x' Traceback (most recent call last):   File "", line 1, in ? TypeError: 'str' object does not support item assignment >>> word[:1] = 'Splat' Traceback (most recent call last):   File "", line 1, in ? TypeError: 'str' object does not support slice assignment</pre>

然而,用组合内容的方法来创建新的字符串是简单高效的:

<pre class="brush:html;toolbar:false">>>> 'x'   word[1:] 'xelpA' >>> 'Splat'   word[4] 'SplatA' 在分切操作字符串时,有一个很有用的规律: s[:i]   s[i:] 等于 s. >>> word[:2]   word[2:] 'HelpA' >>> word[:3]   word[3:] 'HelpA'</pre>

对于有偏差的分切索引的处理方式也很优雅:一个过大的索引将被字符串的大小取代,上限值小于下限值将返回一个空字符串。

<pre class="brush:html;toolbar:false">>>> word[1:100] 'elpA' >>> word[10:] >>> word[2:1]</pre>

在索引中可以使用负数,这将会从右往左计数。例如:

<pre class="brush:html;toolbar:false">>>> word[-1]     # 最后一个字符 'A' >>> word[-2]     # 倒数第二个字符 'p' >>> word[-2:]    # 最后两个字符 'pA' >>> word[:-2]    # 除了最后两个字符之外,其前面的所有字符 'Hel' 但要注意, -0 和 0 完全一样,所以 -0 不会从右开始计数! >>> word[-0]     # (既然 -0 等于 0) 'H'</pre>

超出范围的负数索引会被截去多余部分,但不要尝试在一个单元素索引(非分切索引)里使用:

<pre class="brush:html;toolbar:false">>>> word[-100:] 'HelpA' >>> word[-10]    # 错误 Traceback (most recent call last):   File "", line 1, in ? IndexError: string index out of range</pre>

有一个方法可以让您记住分切索引的工作方式,想像索引是指向字符之间,第一个字符左边的数字是 0。接着,有n个字符的字符串最后一个字符的右边是索引n,例如:

<pre class="brush:html;toolbar:false"> --- --- --- --- ---  | H | e | l | p | A |   --- --- --- --- ---  0   1   2   3   4   5 -5  -4  -3  -2  -1</pre>

第一行的数字 0...5 给出了字符串中索引的位置;第二行给出了相应的负数索引。分切部分从 i 到 j 分别由在边缘被标记为 i 和 j 的全部字符组成。

对于非负数分切部分,如果索引都在有效范围内,分切部分的长度就是索引的差值。例如, word[1:3] 的长度是2。

内置的函数 len() 用于返回一个字符串的长度:

<pre class="brush:html;toolbar:false">>>> s = 'supercalifragilisticexpialidocious' >>> len(s) 34</pre>

您可能感兴趣的文章:
python怎么删除字符
讲解Python3内置模块之json编码解码方法
2019年python学3还是2
python2和3print的区别
php字符串函数有哪些
python u是什么意思
python3中使用什么编码
python2和python3中print有什么区别
php字符串查找函数(strrpos与strchr)
python input是什么意思

[关闭]
~ ~