python 3.7极速入门教程4序列-字符串 列表 元组

本文教程目录

重要的数据结构: 序列

字符串

字符串可以包含在单引号或双引号中。

>>> '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’表示原始字符串,里面的反斜杠不会转义:

>>> r'C:\Program Files\foo\bar\'
  File "<stdin>", line 1
    r'C:\Program Files\foo\bar\'
                               ^
SyntaxError: EOL while scanning string literal
>>> r'C:\Program Files\foo\bar''\\'
'C:\\Program Files\\foo\\bar\\'
>>> 

原始字符串不能以单个反斜杠结尾。换而言之,原始字符串的最后一个字符不能是反斜杠,除非你对其进行转义(但进行转义时,用于转义的反斜杠也将是字符串的一部分)如果最后一个字符(位于结束引号前面的那个字符)为反斜杠,且未对其进行转义,Python将无法判断字符串是否到此结束。

跨行的字符串多使用三引号,即三个单引号或者三个双引号:

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 * 'un' + 'ium'
'unununium'

相邻字符串文本会自动连接,它只用于字符串文本,不能用于字符串表达式和变量(需要使用加号)等:

>>> 'Py' 'thon'
'Python'
>>> prefix 'thon
  File "<stdin>", line 1
    prefix 'thon
               ^
SyntaxError: EOL while scanning string literal
>>> ('un' * 3) 'ium'
  File "<stdin>", line 1
    ('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.'

字符串下标又称索引和C类似 ,第一个字符索引为 0 。没有独立的字符类型,字符就是长度为 1 的字符串,也可以使用负数,-1表示倒数第一个,-2表示倒数第二个,以此类推。不存在的下标会报IndexError。

>>> 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'
>>> word[-16]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> word[16]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range

字符串支持切片:由两个索引,中间是冒号。第一个索引表示起点,包含该元素,默认为0;第2个索引表示终点,不包含该元素,默认为字符串末尾。s[:i] + s[i:]等同于s。

>>> 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'

记住切片的工作方式:切片索引是在字符之间。左边第一个字符的索引为0,右界索引为字符串长度n 。例如:

 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1

第一行数字给出字符串正索引点值0…5 。第二行给出相应的负索引。切片是从 i 到 j 两个数值标示的边界之间的所有字符。

对于非负索引,如果两个索引都在边界内,切片长度就是两个索引之差。例如, word[1:3] 是 2 。

切片时,下标溢出不会报错。

>>> word[4:42]
'on'
>>> word[43:42]
''

Python的字符串是不可变。向字符串文本的某一个索引赋值会引发错误:

>>> word[0] = 'J'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

通过联合(加号)可以简单高效的创建字符串。(注,jython中这种操作并不高效)。

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

常见字符串操作

  • 内置函数len()返回字符串长度:
>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34
  • 替换

方法replace()返回将旧字符串替换为新字符串的副本。

>>> old = "China Testing"
>>> new = old.replace("Testing", "Python")
>>> print(new)
China Python
  • 大小写转换

方法replace()返回将旧字符串替换为新字符串的副本。

>>> s = "China Testing"
>>> print(s.capitalize())
China testing
>>> print(s.upper())
CHINA TESTING
>>> print(s.lower())
china testing
  • 连接

join函数是一种更灵活的串联字符串方式。 使用join函数,您可以在字符串中添加任何字符。

>>> s = "China Testing"
>>> print("-".join(s))
C-h-i-n-a- -T-e-s-t-i-n-g
>>> l = ['1','2','3']
>>> print("".join(l)) # 一种列表转换为字符串的方法
123
  • 反转
>>> s = "https://china-testing.github.io/"
>>> print(''.join(reversed(s)))
/oi.buhtig.gnitset-anihc//:sptth
  • 切割
>>> s = "https://china-testing.github.io/"
>>> s.split('.')
['https://china-testing', 'github', 'io/']

参考资料

字符串试题

1,下面哪个个字符串定义有错误?

A,r’C:\Program Files\foo\bar’
B,r’C:\Program Files\foo\bar’
C, r’C:\Program Files\foo\bar\’
D,r’C:\Program Files\foo\bar\\’

2,min(‘abcd’)的结果是?

A,a B,b |C,c D,d

2,max(‘abcd3A’)的结果是?
A,a B,3 |C,A D,d

元组

元组就像一系列不可变python对象的列表。 列表和元组之间的区别在于列表在方括号中声明,可变;元组在小括号中声明,不可变。

>>> t = ('China', 'Testing')
>>> t
('China', 'Testing')
>>> t = ()
>>> t
()
>>> t = (1,)
>>> t
(1,)
>>> t = (1)
>>> t
1

解包

>>> t = (1, 2)
>>> a, b = t
>>> a
1
>>> t = (1,)
>>> a, = t
>>> a
1

比较元组
Python中的比较运算符可以使用元组。

比较从每个元组的第一个元素开始。如果它们不与=,<或>进行比较,那么它将继续进行第二个元素,依此类推。

它首先比较每个元组的第一个元素

让我们用一个例子研究这个 –

Case1:比较从每个元组的第一个元素开始。在这种情况下,5> 1,因此输出a更大

情况2:比较从每个元组的第一个元素开始。在这种情况下,5> 5是不确定的。所以它进入下一个元素。 6> 4,所以输出a更大

案例3:比较从每个元组的第一个元素开始。在这种情况下,5> 6这是假的。所以它进入了else循环打印“b更大”。

使用元组作为词典中的键
由于元组是可清除的,而list不是,如果我们需要创建一个在字典中使用的复合键,我们必须使用元组作为键。

示例:如果我们需要创建映射,名字,姓氏,电话号码对等的电话簿,我们会遇到一个复合密钥。假设我们已将变量声明为最后一个和第一个数字,我们可以写一个字典赋值语句,如下所示:

    原文作者:python人工智能命理
    原文地址: https://www.jianshu.com/p/5f2dc3eda5bd
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞