py3学习笔记2(字符串)

Posted ZaleJ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了py3学习笔记2(字符串)相关的知识,希望对你有一定的参考价值。

  除了数字计算,python还可以通过多种方式操作字符串

  他们可以被封装在单引号(‘...‘)或者("...")双引号中,使用这两种方式都可以得到相同的结果。(在单引号中,‘\n‘等将不像在双引号中和C语言等其他语言的字符串定义中显示回车,而是显示其本身,只有显示单引号时使用‘\‘‘)

  使用‘\‘可以摆脱引号的引用关系与字符意义。

>>> spam eggs # single quotes
spam eggs
>>> doesn\‘t # use \‘ to escape the single quote...
"doesn‘t"
>>> "doesn‘t" # ...or use double quotes instead
"doesn‘t"
>>> "Yes," he said.
"Yes," he said.
>>> "\"Yes,\" he said."
"Yes," he said.
>>> "Isn\‘t," she said.
"Isn\‘t," she said.

  使用print()函数将获得一个可读性更好的字符串

>>> "Isn\‘t," she said.
"Isn\‘t," she said.
>>> print("Isn\‘t," she said.)
"Isn‘t," she said.
>>> s = First line.\nSecond line. # \n means newline
>>> s # without print(), \n is included in the output
First line.\nSecond line.
>>> print(s) # with print(), \n produces a new line
First line.
Second line.

  如果你需要将‘\‘以及其后的字符显示字符意义,可以在字符串前加‘r‘转义成行字符串

>>> print(C:\some\name) # here \n means newline!
C:\some
ame
>>> print(rC:\some\name) # note the r before the quote
C:\some\name

   有些字符串需要占用许多行,这时可以使用"""..."""或者‘‘‘...‘‘‘行尾将自动地包含在字符串中,也可以加入额外的‘\‘来确保这一点。

print("""Usage: thingy [OPTIONS]
    -h Display this usage message
    -H hostname Hostname to connect to
""")

  以上代码将打印

Usage: thingy [OPTIONS]
    -h Display this usage message
    -H hostname Hostname to connect to

 

  另外,字符串可以使用‘+‘与‘*‘进行操作

>>> # 3 times ‘un‘, followed by ‘ium‘
>>> 3*un + ium
unununium

  两个或多个相邻的字符串将自动组合在一起

>>> Py thon
Python

  但无法使用变量对其进行以上操作

>>> prefix = Py
>>> prefix thon # can‘t concatenate a variable and a string literal
    ...
SyntaxError: invalid syntax
>>> (un*3) ium
    ...
SyntaxError: invalid syntax

  如果你渴望使用变量对字符串进行连接,可以使用‘+‘

>>> prefix + thon
Python

   这个特性可以用于当你想分割一个非常长的句子

>>> text = (Put several strings within parentheses 
...         to have them joined together.)
>>> text
Put several strings within parentheses to have them joined together.    

  你也可以使用类似于其他语言中队数组的索引(index)操作来操作字符串

>>> word = Python
>>> word[0] # character in position 0
P
>>> word[5] # character in position 5
n

  负数将从最后开始倒着索引

>>> word[-1] # last character
n
>>> word[-2] # second-last character
o
>>> word[-6]
P

  当然,-0和0是一样的,负数索引从-1开始。

 

  除了索引,python字符串还支持切片(slice),截取字符串中的一部分

>>> word[0:2] # characters from position 0 (included) to 2 (excluded)
Py
>>> word[2:5] # characters from position 2 (included) to 5 (excluded)
tho

  如下形式的两个切片形式将组成一个完整的字符串

>>> word[:2] + word[2:]
Python
>>> word[:4] + word[4:]
Python

  当冒号左侧没有数字时默认将从头开始,右侧没有数字时将读到末尾

>>> word[:2] # character from the beginning to position 2 (excluded)
Py
>>> word[4:] # characters from position 4 (included) to the end
on
>>> word[-2:] # characters from the second-last (included) to the end
on

  

  你可以用以下这种方法简介地记住切片的位置

技术分享

  

  使用index方法访问超过数组长度的元素将返回错误

>>> word[42] # the word only has 6 characters
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range

  而使用slice方法将不受限制,超出时将默认为字符串末尾,没有内容时将返回空字符串

>>> word[4:42]
on
>>> word[42:]
‘‘

 

  注意,在python中,字符串一旦被定义将无法被改变,以下试图改变字符串内容的行为都将报错

>>> word[0] = J
    ...
TypeError: str object does not support item assignment
>>> word[2:] = py
    ...
TypeError: str object does not support item assignment

  如果你需要一个不一样的字符串,只能创建一个新的

>>> J + word[1:]
Jython
>>> word[:2] + py
Pypy

  

  另外,你也可以使用len()函数来获取一个字符串的长度

>>> s = supercalifragilisticexpialidocious
>>> len(s)
34

 

以上是关于py3学习笔记2(字符串)的主要内容,如果未能解决你的问题,请参考以下文章

py3学习笔记3(列表)

py3学习笔记0(入坑)

学习笔记:python3,代码片段(2017)

js-权威指南学习笔记14

Python 学习笔记之字符串

Python 3学习笔记