主要内容

  • 字符串的使用说明

除了数字,Python还可以操作字符串。
字符串: 由单引号(’…’)或双引号(”…”)括起来即为字符串。
转义引号:使用 \

操作

  1. >>> 'spam eggs' # 单引号
  2. 'spam eggs'
  3. >>> 'doesn\'t' # 使用 \ 对单引号进行转译
  4. "doesn't"
  5. >>> "doesn't" # ...字符串中有单引号时 最外层使用双引号
  6. "doesn't"
  7. >>> '"Yes," they said.' # 字符串中有双引号时 外层使用单引号
  8. '"Yes," they said.'
  9. >>> "\"Yes,\" they said." # 同时使用双引号时 \ 作为转译字符
  10. '"Yes," they said.'
  11. >>> '"Isn\'t," they said.' # 最外层使用单引号,内部使用双引号时不再转译
  12. '"Isn\'t," they said.'

字符串中的换行符使用 \n

  1. >>> '"Isn\'t," they said.'
  2. '"Isn\'t," they said.'
  3. >>> print('"Isn\'t," they said.')
  4. "Isn't," they said.
  5. >>> s = 'First line.\nSecond line.' # \n 回车 新换行
  6. >>> s # 不使用print() 函数 默认输出字符圆形
  7. 'First line.\nSecond line.'
  8. >>> print(s) # 使用 print()打印, \n 将换行
  9. First line.
  10. Second line.

如果不希望将前缀为 \ 的字符解释为特殊字符,在第一个引号之前添加 r 来使用原始字符串:

  1. >>> print('C:\some\name') # 这里 \n 会新换一行
  2. C:\some
  3. ame
  4. >>> print(r'C:\some\name') # 添加 r 前缀
  5. C:\some\name

多行显示 使用 """....""" 或者 '''....''' 方式

  1. >>> print("""\
  2. ... Usage: thingy [OPTIONS]
  3. ... -h Display this usage message
  4. ... -H hostname Hostname to connect to
  5. ... """)
  6. Usage: thingy [OPTIONS]
  7. -h Display this usage message
  8. -H hostname Hostname to connect to

字符串之间使用 + 表示进行拼接,使用 * 后跟数字进行多次

  1. >>> # 打印3次 'un', 后面连接 'ium'
  2. >>> 3 * 'un' + 'ium'
  3. 'unununium'

两个或多个字符串之间使用空格也可以进行连接

  1. >>> 'Py' 'thon'
  2. 'Python'

当长字符串断开,使用 () 进行连接

  1. >>> text = ('Put several strings within parentheses '
  2. ... 'to have them joined together.')
  3. >>> text
  4. 'Put several strings within parentheses to have them joined together.'

注意:使用空格连接字符串只能是 ‘xxx’ ‘xxxx’ 这种形式,如果用变量 ‘xxx’ 会报错。

  1. >>> prefix = 'Py'
  2. >>> prefix 'thon' # 不能用变量空格连接字符串
  3. File "<stdin>", line 1
  4. prefix 'thon'
  5. ^
  6. SyntaxError: invalid syntax
  7. >>> ('un' * 3) 'ium'
  8. File "<stdin>", line 1
  9. ('un' * 3) 'ium'
  10. ^
  11. SyntaxError: invalid syntax

字符串可以使用索引来访问其中的值,默认索引从0开始

  1. >>> word = 'Python'
  2. >>> word[0] # 索引为0
  3. 'P'
  4. >>> word[5] # 索引为5
  5. 'n'

索引值使用负数,表示从后往前数

  1. >>> word[-1] # 最后一个
  2. 'n'
  3. >>> word[-2] # 倒数第二个
  4. 'o'
  5. >>> word[-6]
  6. 'P'

同样,也可以从字符串中切片取其中的部分字符

  1. >>> word[0:2] # 从索引0(包含索引0)开始到索引2(不包含索引2)之间的字符
  2. 'Py'
  3. >>> word[2:5] # 从索引2(包含索引2)开始到索引5(不包含索引5)之间的字符
  4. 'tho'

s[:x] 表示从索引0开始到索引x之间的字符(不包含索引x)
s[x:] 表示从索引x开始到最后的字符(包含索引x)

  1. >>> word[:2]
  2. 'Py'
  3. >>> word[2:]
  4. 'thon'
  5. >>> word[:2] + word[2:]
  6. 'Python'
  7. >>> word[-2:] # 最后2位
  8. 'on'
  1. +---+---+---+---+---+---+
  2. | P | y | t | h | o | n |
  3. +---+---+---+---+---+---+
  4. 0 1 2 3 4 5 6
  5. -6 -5 -4 -3 -2 -1

索引值超出文本长度会报错

  1. >>> word[42] # word变量的长度只有6
  2. Traceback (most recent call last):
  3. File "<stdin>", line 1, in <module>
  4. IndexError: string index out of range

在对文本进行切片时超出长度时取值时如果超出长度返回’’

  1. >>> word[4:42]
  2. 'on'
  3. >>> word[42:]
  4. ''

字符串一旦赋值,不能通过索引更改其中的字符,否则会报错。

  1. >>> word[0] = 'J'
  2. Traceback (most recent call last):
  3. File "<stdin>", line 1, in <module>
  4. TypeError: 'str' object does not support item assignment
  5. >>> word[2:] = 'py'
  6. Traceback (most recent call last):
  7. File "<stdin>", line 1, in <module>
  8. TypeError: 'str' object does not support item assignment

使用 + 可以连接不同的字符创建新字符串

  1. >>> 'J' + word[1:]
  2. 'Jython'
  3. >>> word[:2] + 'py'
  4. 'Pypy'

使用 len() 函数可以获取字符串的长度。

  1. >>> s = 'supercalifragilisticexpialidocious'
  2. >>> len(s)
  3. 34