目录
字符串本质是字符序列。
Python的字符串是不可修改的。无法对原字符串进行修改,但是可以将字符串的一部分赋值到新字符串,来达到相同的修改效果。
符串或串(String)是由零个或多个字符组成的有限序列。它是编程语言中表示文本的数据类型。python中字符串的表现形式为包含在单引号或者双引号中的字符序列。
- >>> a = "abcd"
- >>> len(a)
- 4
使用+可以将多个字符串或字符串变量拼接起来,也可以直接将一个字符串字面量放到另一个的后面直接实现拼接。
使用*可以进行字符串复制。
- >>> a = "ab"
- >>> b = "ac"
- >>> a + b
- 'abac'
-
- >>> a * 2
- 'abab'
在字符串名后面添加[],并在括号里指定偏移量可以提取该位置的单个字符。
注意:
第一个字符的便宜量为0,下一个是1,以此类推。最后一个字符的偏移量可以使用-1来表示,这样就不必从头数到尾。偏移量从右向左紧接着为-2、-3,以此类推。
如果指定的字符串超过了字符串的长度,会得到一个错误提示:string index out of range.
- >>> a = "abcdefg"
- >>> a[0]
- 'a'
- >>> a[1]
- 'b'
- >>> a[-1]
- 'g'
- >>> a[10]
- Traceback (most recent call last):
- File "<input>", line 1, in <module>
- IndexError: string index out of range
从一个字符串抽取一个子字符串(字符串的一部分)。我们使用一对方括号,起始偏移量start,和终止偏移量end以及可选的步长step来定义一个分片。其中一些参数可以省略。分片将会得到start开始到end结束之前全部的字符。
- >>> a = 'abcdefgh'
- >>> a[:]
- 'abcdefgh'
- >>> a[2:]
- 'cdefgh'
- >>> a[:5]
- 'abcde'
- >>> a[2:5]
- 'cde'
- >>> a[1:6:2]
- 'bdf'
split()可以基于分隔符将字符串分割成若干子串组成的列表
- >>> s = "ab,cd,ef,ghk"
- >>> s.split(",")
- ['ab', 'cd', 'ef', 'ghk']
replace()函数可以将指定的字符串替换其他的字符串,并生成新的字符串。
replace(old,new,count)
old:被替换的内容
new:替换内容
count:最多替换次数,可不传参数
- s = "ab,cd,ef,ghk"
-
- >>> s.replace(',', '1')
- 'ab1cd1ef1ghk'
- >>> s.replace(',', '1', 2)
- 'ab1cd1ef,ghk'
- >>> a = 'python'
- >>> a.capitalize()
- 'Python'
- >>> b = 'hello python'
- >>> b.title()
- 'Hello Python'
- >>> c = 'python'
- >>> c.upper()
- 'PYTHON'
- >>> d = 'PYTHON'
- >>> d.lower()
- 'python'
- 1.2.8 字符串遍历
- my_string = "abcdefg"
- # 1. 遍历字符串while方式
- index = 0
- while index < len(my_string):
- print(my_string[index], end="|")
- index += 1
-
- print()
- # 2. 遍历字符串for方式
- for ch in my_string:
- print(ch, end="|")
列表非常适合利用顺序和位置定义某一个元素,尤其是当元素的顺序或内容经常发生改变的时候。与字符串不同,列表中元素的值是可以修改的。我们可以直接对列表中进行添加新元素、删除元素、或覆盖已有的元素。
可以使用[]来创建一个列表,也可以使用list函数来创建一个空列表。
- list1 = []
- list2 = list()
一个列表中的元素又是一个列表,那么这就是列表的嵌套。
schoolNames = [ ['北京大学', '清华大学'], ['南开大学', '天津大学', '天津师范大学'], ['山东大学', '中国海洋大学'] ]
遍历列表中的元素,我们可以使用while循环或者for循环来遍历,如下:
- namelist = ["Edward", "Smith", "John", "Obama", "Polly"]
-
- # 1. while循环方式
- index = 0
- while index < len(namelist):
- print(namelist[index], end=" ")
- index += 1
- print()
-
- # 2. for循环方式
- for name in namelist:
- print(name, end=" ")
append()函数将元素一个个添加到尾部
- >>> list = [1, 'a']
- >>> list.append(2)
- >>> print(list)
- [1, 'a', 2]
extend()可以将一个列表合并到另一个列表中,我们也可以使用+=
- >>> list1 = ['a', 1]
- >>> list2 = ['b', 2]
- >>> list3 = ['c', 3]
- >>> list1.extend(list2)
-
- >>> print(list1)
- ['a', 1, 'b', 2]
- >>> list3 += list2
- >>> print(list3)
- ['c', 3, 'b', 2]
append()只能将新元素插入到列表尾部,insert()可以将元素插入到列表的任意位置。
insert(index,object),index插入元素的位置,object要插入的元素
指定index参数为0,就可以将元素插入到列表头部,如果指定的第一个参数偏移量大于列表长度,则会插入到列表尾部。
- >>> list1 = ['a', 1]
- >>> list1.insert(1, 'b')
- >>> print(list1)
- ['a', 'b', 1]
- >>> list1.insert(5, 2)
- >>> print(list1)
- ['a', 'b', 1, 2]
del是python语句,而不是列表方法,无法通过list来调用。使用del可以删除一个元素,当元素删除之后,位于它后面的元素会自动移动填补空出来的位置。
- >>> list1 = ['a', 'b', 1, 2]
- >>> del list1[1]
- >>> print(list1)
- ['a', 1, 2]
如果不确定或不关心元素在列表中的位置,可以使用remove()根据指定的值来删除元素。
- >>> list1 = ['a', 1, 'b']
- >>> list1.remove('b')
- >>> print(list1)
- ['a', 1]
使用pop()同样可以获取列表中指定位置的元素,但在获取完成之后,该元素会自动被删除。
如果为pop(off)指定了偏移量,它会返回偏移量对应位置的元素。
如果不指定,则默认使用-1。因此pop(0)将返回头元素,而pop()或pop(-1)则会返回列表的尾元素。
- >>> list1 = ['1', 'a', 2, 'b', 'c', 'd']
- >>> list1.pop(1)
- 'a'
- >>> print(list1)
- ['1', 2, 'b', 'c', 'd']
- >>> list1.pop()
- 'd'
- >>> print(list1)
- ['1', 2, 'b', 'c']
如果想知道等于某一个值的元素在列表中的位置,可以使用index()查询
- >>> list1 = ['1', 'a', 2, 'b', 'c', 'd']
- >>> list1.index('b')
- 3
count()可以记录某一个元素在列表中出现的次数
- >>> list1 = ['a', 'b', 'a', 'c', 'd']
- >>> list1.count('a')
- 2
join其实是一个string的方法,而不是列表方法,不能通过list.join(“,”)进行调用
- >>> list1 = ['aa', 'bb', 'cc']
- >>> "".join(list1)
- 'aabbcc'
- >>> "-".join(list1)
- 'aa-bb-cc'
- >>> list1 = [n for n in range(5)]
- >>> print(list1)
- [0, 1, 2, 3, 4]
与列表类似,元组也是由任意类型元素组成的序列。
与列表不同的是,元组是不可改变,这意味着一旦元组被定义,将无法再进行增加、删除或修改元素等操作。因此,元组就像一个不可改变的列表.
在许多地方元组都可以替换列表,但元组的方法函数比列表要少一些,元组没有append()、insert()等等,因为一旦创建元组便无法修改。
既然列表更加灵活,那为什么不在所有地方都使用列表呢?
- # 创建元组
- my_tuple = (1, 2, 3, 4, 5)
-
- # 访问元组中的元素
- print(my_tuple[0])
- print(my_tuple[1])
-
- # while循环访问元组中元素
- index = 0
- while index < len(my_tuple):
- print(my_tuple[index])
- index += 1
-
- # 遍历元组
- my_list1 = ("John", 12, "Obama", 88)
- for ein my_list1:
- print(e)