(一)元组
(1)元组的定义
元组与列表相似,不过元组的元素为只读的,不能修改。
创建元组,可以使用如下格式:
#定义一个包含四个元素的元组tup1 = ("do","it", "right", "now")#定义一个包含数据库节点地址,端口号,用户名,密码的元组mysql_connection = ("localhost", "3306", "root", "123456")#定义一个空的元组tup2 = ()#定义一个包含一个元素的元组,需要在元素后面添加逗号,否则括号会被当成运算符,引起混淆tup3 = (12,)tup4 = ("be mercy",)
(2) 访问元组
格式: 元组名称[下标索引]
(3)元组的运算
元组运算方式基本与列表相同,但是不能对元组的单个元素进行删除或者修改,元组是只读的,只能对元组整体进行删除
| 运算符 | 说明 |
|---|---|
| + | 连接两个元组 |
| * | 重复多次输出元组的内容 |
| del() | 删除元组 |
| len() | 测试元组的长度 |
| max() | 返回元组中元素最大值 |
| min() | 返回元组中元素最小值 |
| tuple() | 将列表转换为元组 |
| in | 判断元素在元组中否 |
(4)举例
tu1=("go", "forward")tu2=("that's ", "it")#合并元组tu1和tu2,并合并tu3=tu1 + tu2print("tu3={}".format(tu3))#将元组tu3扩展三倍print("tu3={}".format(tu3*3))print(len(tu3))tu4={1,8,290,11,15}#打印tu4元组的最大值print(max(tu4))#打印tu4元组的最小值print("minimum={:d}".format(min(tu4)))#遍历tu3元组for x in tu3: print(x)
运行结果为:
tu3=('go', 'forward', "that's ", 'it')tu3=('go', 'forward', "that's ", 'it', 'go', 'forward', "that's ", 'it', 'go', 'forward', "that's ", 'it')4290minimum=1goforwardthat'sit
(二)字典
(1)关于字典及其定义
字典是一组key-value键值对,类似于jason格式的数据。
1) 定义一个字典可以使用以下格式:
dict1 = {'Name':'Susan'}dict2 = {"stu_no":"17080021","name":"John"}#定义一个空字典dict3 = {}
2) 使用dict.copy()方法进行浅复制
所谓的浅复制,实质上是对原有变量的引用,复制以后的变量类似于:
- windows中的快捷方式
- linux中的软链接
- c语言中的指向变量地址的引用
浅复制实际上是对被复制的对象的一种别名引用,因而当被复制对象发生了改变之后,浅复制生成对象同样发生了该表
在python中,对列表、字典变量的直接赋值,都属于浅复制。
而dict.copy()方法对第一层对象来说,是深复制;而第二层对象,同样是浅复制
例如:
dict1 = {"location": {"state": "Massachussets", "city": "Cambridge"},"address": "No. 77, Massachessets Avenue", "zipcode": "02139"}#dict2为dict1的浅复制dict2 = dict1#dict3对于dict1来说,第一层是深复制,第二层是浅复制dict3 = dict1.copy()#修改zipcodedict1["zipcode"] = "02139-4307"#在location的成员字典中增加一个nationdict1["location"]["nation"]="The United States of American"#修改locaiton中的city的值dict1["location"]["city"]="Cambridge city"#增加一个tel键dict1["Tel"]="617-253-1000"print("dict1={}".format(dict1))print("dict2={}".format(dict2))print("dict3={}".format(dict3))
运行后,结果为:
dict1={'location': {'state': 'Massachussets', 'city': 'Cambridge city', 'nation': 'The United States of American'}, 'address': 'No. 77, Massachessets Avenue', 'zipcode': '02139-4307', 'Tel': '617-253-1000'}dict2={'location': {'state': 'Massachussets', 'city': 'Cambridge city', 'nation': 'The United States of American'}, 'address': 'No. 77, Massachessets Avenue', 'zipcode': '02139-4307', 'Tel': '617-253-1000'}dict3={'location': {'state': 'Massachussets', 'city': 'Cambridge city', 'nation': 'The United States of American'}, 'address': 'No. 77, Massachessets Avenue', 'zipcode': '02139'}
dict2是dict1的浅复制,所以dict1发生改变,则dict2同样发生改变
dict3是dict1的copy复制,第一层深复制,第二层浅复制,则对dict1的第一层的该表,如:zipcode还有Tel,都没有复制到dict3,但是dict2同样发生了改变。
而location的值是一个字典,属于第二层,则对location的值的该表,同样传导到了dict2和dict3,因为对于第二层而言,无论是变量直接赋值,还是用copy()方法,都属于浅复制。
(2)字典值的访问,字典中键、值以及键值对的取出
1)字典中值的访问
键放入方括号内,可以实现对字典中值得访问,例如:
dict1 = {"index":1, "buck":12, "title":"Management Science", "author":"Smith"}print("the titile of dict1 is:{}".format(dict1["title"]))
运行结果为:
The titile of dict1 is:Management Science
也可以使用get方法,来实现对键值的访问,例如:
dict1 = {"index":1, "buck":12, "title":"Management Science", "author":"Smith"}#指定获取buck键对应的值s1 = dict1.get("buck")#如果指定的Tel键不存在,则设置返回值为10086s2=dict1.get("Tel","10086")print(s1,s2)
结果为
12 10086
2)字典中键、值以及键值对的取出
- dict.keys()方法可以取出一个字典变量的所有键
- dict.values()方法可以取出一个字典变量的所有值
- dict.items()方法可以取出每一个key-value键值对,并把每一个键值对放到一个元组中
例如:
dict1 = {"index":1, "buck":12, "title":"Management Science", "author":"Smith"}#取出键i = 0for key in dict1.keys():i +=1print("{:d}.key={}".format(i,key))#取出值j=0for value in dict1.values():j += 1print("{:d}.value=\"{}\"".format(j,value))k=0#取出键值对for item in dict1.items():k += 1print("Item {:d} is: {}".format(k,item))
运行结果为:
1.key=index2.key=buck3.key=title4.key=author1.value="1"2.value="12"3.value="Management Science"4.value="Smith"Item 1 is: ('index', 1)Item 2 is: ('buck', 12)Item 3 is: ('title', 'Management Science')Item 4 is: ('author', 'Smith')
由于items方法将结果以元组的形式展现,因而我们也可以通过并列赋值的方式,将key和value分别赋给单独的变量,例如:
dict1 = {"index":1, "buck":12, "title":"Management Science", "author":"Smith"}#将键值对的值分别赋给key和value变量for key,value in dict1.items():print("key={},value={}".format(key,value))
运行结果为:
key=index,value=1key=buck,value=12key=title,value=Management Sciencekey=author,value=Smith
3)dict.setdefault(key,default)方法
setdefault方法同样可以获取一个key对应的值,但是setdefault方法在获取key对应值的同时,还有设置功能,如果这个key在字典中不存在,则可以设置到字典中
例如:
dict1={"grade":"17","class":"MS1701","Name":"Susan"}#获得name对应的值get1=dict1.setdefault("Name")print("get1=%s" % get1)#sex这个键并不存在,可以自动加入到字典中,并指定默认值为Femaleget2 = dict1.setdefault("Sex","Female")print("get2=%s" % get2)print("dict1={}".format(dict1))
运行结果为:
get1=Susanget2=Femaledict1={'grade': '17', 'class': 'MS1701', 'Name': 'Susan', 'Sex': 'Female'}
(3)增加键值对、修改和删除已有键对应的值
1)增加键值对,仅仅需要直接定新增键和值的定义即可
例如:
dict1={"index":1, "buck":12, "title":"Management Science", "author":"Smith"}#新增一个school键dict1["school"]="Harvard University"print("dict1={}".format(dict1))
运行结果为:
dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'Harvard University'}
2) 修改已有的键值对
i) 只需对之重新赋值,就可以修改已经存在的键对应的值。
例如:
dict1={"index":1, "buck":12, "title":"Management Science", "author":"Smith"}#新增一个school键dict1["school"]="Harvard University"#对school键对应的值进行修改dict1["school"] = "California Institute of Technology"#打印结果,可得print("dict1={}".format(dict1))
运行以后,可得
dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}
ii) dict.update(dict2)方法
可以把dict2的键值对更新到dict中,如果dict2中的键已经在dict中存在,则是更新;否则,是增加。例如:
dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}dict2={"school":"Massachussets Institue of Technology ", "college":"Management Science and Engineering"}#用dict2更新dict1,对于已经在dict1中存在的school键,是替换;对于在dict1中不存在的college键,则是增加dict1.update(dict2)print("dict1={}".format(dict1))
运行结果为:
dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'Massachussets Institue of Technology ', 'college': 'Management Science and Engineering'}
3) 删除已有的键值对
i)del(),删除已有的键值对,例如
dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}print("dict1={}".format(dict1))# 删除buck键对应的值del dict1['buck']print("dict1={}".format(dict1))
运行结果为
dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}dict1={'index': 1, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}
ii)dict.clear()方法,将整个字典内容清空
dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}print("dict1={}".format(dict1))#置空dict1字典dict1.clear()#现在打印出来的dict1只是一个空字典了print("dict1={}".format(dict1))
运行结果为:
dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}dict1={}
iii)dict.pop(key)方法,删除给定键key对应的值,返回值为被删除的值,key值必须给出。否则,返回default值。
例如:
dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}print("dict1={}".format(dict1))dict1.pop("buck")print("dict1={}".format(dict1))
运行结果为:
dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}dict1={'index': 1, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}
iv) dict.popitem()方法,随机返回并删除字典中的一对键和值(一般删除末尾一对键值)
dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}print("dict1={}".format(dict1))#删除了末尾的一对键值dict1.popitem()print("dict1={}".format(dict1))
运行结果为:
dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith', 'school': 'California Institute of Technology'}dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith'}
(4)字典长度和对字符串的转换
- len()字典长度
- str(dict)将字典转换为字符串
例如:
dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith'}dict1={'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith'}print("the length of dict1 is:%d" % len(dict1))print(str(dict1))
运行结果为:
the length of dict1 is:4{'index': 1, 'buck': 12, 'title': 'Management Science', 'author': 'Smith'}
(5)从元组和列表转换为字典
fromkeys()方法可以将一个序列转换为字典,序列中的各个值为字典中的键,而如果指定了默认值,则字典中的各个键值为默认值,否则为None
举例如下:
# 初始化一个元祖seq1=("1","3","note","that")#初始化一个字典dict1={}#注意:fromkeys方法本身返回一个新的字典,但是不改变dict1的值。要将返回的新的字典赋给一个变量保存#1为default值dict1 = dict1.fromkeys(seq1, 1)print("dict1=%s" % dict1)seq2=("2","4","do","it")#fromkey方法对原有的字典的内容不保存,使用指定的序列生成一个新的字典,完全替换掉原有内容,并返回dict1 = dict1.fromkeys(seq2, 2)print("dict1=%s" % dict1)#也可以从列表生成list3=["Name","No","grade","class"]dict1 = dict1.fromkeys(list3,3)print("dict1=%s" % dict1)
运行结果如下:
dict1={'1': 1, '3': 1, 'note': 1, 'that': 1}dict1={'2': 2, '4': 2, 'do': 2, 'it': 2}dict1={'Name': 3, 'No': 3, 'grade': 3, 'class': 3}
(三)集合
(1)集合的创建
集合是一个无序不重复元素的集。基本功能包括关系测试和消除重复元素。
创建集合,可以使用大括号括起来的元素列表,或者用set(),但是请注意:如果要创建空的集合,必须使用set(),而不能用{},因为这会导致创建一个空字典,而不是空集合。
以下为几个示例:
list1 = ["Life", "was", "like", "a", "box", "of", "chocolates,","you", "never", "know", "what", "you're", "gonna", "get"]tu1 = (1,8,0,67,12,0,8,11,8,1)#直接定义集合,定义中的重复元素会被自动去掉set1 = {"Today", "I", "consider", "myself","the", "luckiest", "man", "on", "the", "face", "of", "the", "Earth"}#由元组转化,重复元素去掉set2 = set(tu1)#由列表转化,重复元素去掉set3 = set(list1)#通过打印出来的结果,可以看到,集合是无序的print("set1=%s" % set1)print("set2={}".format(set2))print("set3={}".format(set3))#创建一个空的集合set4 = set()
列表、元组中的元素值可以用下标获取,字典中的值可以用键获取,但是集合中的值不能用下标获取。
可以考虑将集合转化为列表或者元组之后,再设法取得,转化例子如下:
list1 = ["Life", "was", "like", "a", "box", "of", "chocolates,","you", "never", "know", "what", "you're", "gonna", "get"]tu1 = (1,8,0,67,12,0,8,11,8,1)set2 = set(tu1)set3 = set(list1)#将set2转化为列表list2 = list(set2)print("list2=%s" % list2)#将set3转化为元组tu2 = tuple(set3)print("tuple2={}".format(tu2))
运行结果为:
list2=[0, 1, 67, 8, 11, 12]tuple2=('like', 'box', 'you', 'was', 'chocolates,', 'know', 'Life', "you're", 'never', 'of', 'get', 'a', 'what', 'gonna')
(2)集合的增删查
1)判断元素是否在集合中
同列表、元组和字典一样,在时候用集合的时候,我们同样可以用in来判断元素是否在集合中
例如:
set1 = {"Today", "I", "consider", "myself","the", "luckiest", "man", "on", "the", "face", "of", "the", "Earth"}# in判断set1中是否有元素"Today"if "Today" in set1:print("True")else:print("False")
运行结果为:
True
类似于列表、元组与字典,使用in关键字同样也可以对set进行遍历,例如:
et1 = {"Today", "I", "consider", "myself","the", "luckiest", "man", "on", "the", "face", "of", "the", "Earth"}j = 0for item in set1:j += 1print("item%d=%s" % (j,item))
运行结果为:
item1=consideritem2=Todayitem3=onitem4=theitem5=myselfitem6=faceitem7=Earthitem8=luckiestitem9=manitem10=Iitem11=of
2)add()方法添加元素到集合
由于列表是有序的,因而在列表中有append、extend以及insert等添加元素的方法,但是set是无序的,因而在set中添加元素只有add和update方法。
set1 = {"Today", "I", "consider", "myself","the", "luckiest", "man", "on", "the", "face", "of", "the", "Earth"}# 注意:add方法的参数不可以是列表#set1.add(["do", "it"])# add方法的参数也不可以是字典#set1.add({"Number":15})#add方法的参数可以是元组set1.add(("do", "it"))#add可以多次添加set1.add("Not")print("set1={}".format(set1))
运行结果为:
set1={'Not', 'on', 'myself', 'the', 'I', 'Earth', 'consider', 'man', 'luckiest', 'Today', 'of', ('do', 'it'), 'face'}
3)update()方法添加元素
update方法是一个相当全能的方法,对字典、字符串、列表和元组都可以处理,其特点如下:
- update可以实现叠加操作,每次update都会对原集合进行处理
- 如果update的操作对象是元组,则元组的每个元素独立加入set,成为set的一个新元素
- 如果update的操作对象是字符串,则字符串中的每个字符独立加入set,成为set的一个新元素
- 如果update的操作对象是列表,则列表中的每一项都独立加入set,成为set的一个新元素
- 如果update的操作对象是字典,则字典中的每一个键都会独立加入set,成为set的一个新元素,而值被舍弃
例如:
set1 = {"Today", "I", "consider", "myself","the", "luckiest", "man", "on", "the", "face", "of", "the", "Earth"}tu1=("I", "am", "a", "monster", "on", "the", "hill")list1=["the", "luckiest", "one"]#元组中每个元素独立加入set1.update(tu1)print("set1={}".format(set1))#列表中每个元素独立加入set1.update(list1)print("set1={}".format(set1))#字符串中每个字符独立加入str1 = "No more than five years"set1.update(str1)print("set1={}".format(set1))#字典中每个键独立加入,值舍去dict1 = {"Number": 123, "Name": "zs"}set1.update(dict1)print("set1={}".format(set1))
结果为:
set1={'on', 'consider', 'myself', 'a', 'monster', 'face', 'luckiest', 'I', 'of', 'Earth', 'hill', 'the', 'Today', 'am', 'man'}set1={'on', 'consider', 'myself', 'a', 'monster', 'face', 'luckiest', 'I', 'of', 'Earth', 'hill', 'one', 'the', 'Today', 'am', 'man'}set1={'r', 'N', 'of', 'one', 'v', ' ', 'myself', 'I', 'hill', 'e', 'the', 'Today', 'm', 't', 'man', 's', 'o', 'on', 'monster', 'luckiest', 'Earth', 'h', 'i', 'n', 'f', 'consider', 'face', 'y', 'am', 'a'}set1={'Name', 'r', 'N', 'of', 'one', 'v', ' ', 'myself', 'I', 'hill', 'e', 'the', 'Today', 'm', 't', 'man', 's', 'o', 'on', 'monster', 'luckiest', 'Earth', 'h', 'i', 'n', 'f', 'consider', 'face', 'y', 'am', 'a', 'Number'}
从结果中,我们也可以发现集合的两个重要特点
- 1.无序
- 2.去重
4)remove()方法,移除元素
remove()方法允许通过指定元素值的方式,将元素从集合中移除。例如:
set1={'on', 'consider', 'myself', 'a', 'monster', 'face', 'luckiest', 'I', 'of', 'Earth', 'hill', 'one', 'the', 'Today', 'am', 'man'}print("set1={}".format(set1))set1.remove('face')print("set1={}".format(set1))
运行结果为:
set1={'one', 'I', 'of', 'luckiest', 'on', 'myself', 'face', 'the', 'monster', 'man', 'am', 'Today', 'Earth', 'a', 'hill', 'consider'}set1={'one', 'I', 'of', 'luckiest', 'on', 'myself', 'the', 'monster', 'man', 'am', 'Today', 'Earth', 'a', 'hill', 'consider'}
如果被移除的值不存在,则会发生异常,例如:
set1={'on', 'consider', 'myself', 'a', 'monster', 'face', 'luckiest', 'I', 'of', 'Earth', 'hill', 'one', 'the', 'Today', 'am', 'man'}print("set1={}".format(set1))set1.remove('me')print("set1={}".format(set1))
运行结果为:
Traceback (most recent call last):File "C:/Users/thomas/PycharmProjects/py_v3/Lesson3_exercise.py", line 113, in <module>set1.remove('me')KeyError: 'me'
5)discard()方法移除元素
discard方法也允许从集合中移除元素,而且在指定的元素不存在的时候不会报错。例如:
set1={'on', 'consider', 'myself', 'a', 'monster', 'face', 'luckiest', 'I', 'of', 'Earth', 'hill', 'one', 'the', 'Today', 'am', 'man'}print("set1={}".format(set1))set1.discard('consider')print("set1={}".format(set1))
运行结果为:
set1={'monster', 'of', 'am', 'on', 'one', 'the', 'man', 'face', 'Today', 'hill', 'I', 'a', 'consider', 'luckiest', 'myself', 'Earth'}set1={'monster', 'of', 'am', 'on', 'one', 'the', 'man', 'face', 'Today', 'hill', 'I', 'a', 'luckiest', 'myself', 'Earth'}
如果是不存在的元素,则有:
set1={'on', 'consider', 'myself', 'a', 'monster', 'face', 'luckiest', 'I', 'of', 'Earth', 'hill', 'one', 'the', 'Today', 'am', 'man'}print("set1={}".format(set1))set1.discard('me')print("set1={}".format(set1))
运行结果为:
set1={'luckiest', 'the', 'man', 'of', 'consider', 'I', 'Earth', 'face', 'a', 'on', 'myself', 'hill', 'one', 'Today', 'monster', 'am'}set1={'luckiest', 'the', 'man', 'of', 'consider', 'I', 'Earth', 'face', 'a', 'on', 'myself', 'hill', 'one', 'Today', 'monster', 'am'}
可见,当使用discard之后,如果指定了一个不存在的元素,discard方法就什么都不做,也不报错。
6)pop()方法随机删除元素
pop方法允许从集合中随机删除一个元素,例如:
set1={'on', 'consider', 'myself', 'a', 'monster', 'face', 'luckiest', 'I', 'of', 'Earth', 'hill', 'one', 'the', 'Today', 'am', 'man'}print("set1={}".format(set1))set1.pop()print("set1={}".format(set1))
运行结果为:
set1={'one', 'on', 'Earth', 'am', 'myself', 'consider', 'I', 'the', 'a', 'hill', 'monster', 'face', 'man', 'luckiest', 'Today', 'of'}set1={'on', 'Earth', 'am', 'myself', 'consider', 'I', 'the', 'a', 'hill', 'monster', 'face', 'man', 'luckiest', 'Today', 'of'}
(3)集合的并交差运算
集合可以进行并交叉等各种运算,如:
set_a = {'cucumber', 'cabbage', 'tomato'}set_b = {'apple', 'banana', 'tomato'}#并集运算,取两个集合之和set_union = set_a | set_bprint("set_union={}".format(set_union))#交集运算,去两个集合的交集set_intersection = set_a & set_bprint("set_intersection={}".format(set_intersection))#差集运算,取在第一个集合中存在,但是第二个集合中不存在的元素set_difference = set_a - set_bprint("set_difference={}".format(set_difference))#对称差集运算,取在两个集合中存在,但不同时存在的元素set_symmetric_difference = set_a ^ set_bprint("set_symmetric_difference={}".format(set_symmetric_difference))#并集运算set_union = set_a.union(set_b)print("set_union={}".format(set_union))#交集运算set_intersection = set_a.intersection(set_b)print("set_intersection={}".format(set_intersection))#差集运算set_difference = set_a.difference(set_b)print("set_difference={}".format(set_difference))#对称差集运算set_symmetric_difference = set_a.symmetric_difference(set_b)print("set_symmetric_difference={}".format(set_symmetric_difference))
运算结果为:
set_union={'cabbage', 'apple', 'cucumber', 'tomato', 'banana'}set_intersection={'tomato'}set_difference={'cabbage', 'cucumber'}set_symmetric_difference={'cabbage', 'cucumber', 'apple', 'banana'}set_union={'cabbage', 'apple', 'cucumber', 'tomato', 'banana'}set_intersection={'tomato'}set_difference={'cabbage', 'cucumber'}set_symmetric_difference={'cabbage', 'cucumber', 'apple', 'banana'}
(四)字符串函数
Python内建的字符串处理函数非常丰富,为字符串处理提供了有力的工具。
这里试列举一些字符串处理函数
| 名称 | 描述 |
|---|---|
| capitalize() | 将字符串的第一个字符转换为大写 |
| center(width,fillchar) | 返回一个指定宽度为width居中的字符,fillchar为填充的字符,默认为空格 |
| count(str, beg=0, end=len(string)) | 返回str在string中出现的次数,如果beg或者end指定,则返回指定范围内出现str的次数 |
| encode(encoding=’UTF-8’,errors=”strict”) | 以encoding指定的编码格式编码字符串,如果出错默认报一个ValueError的异常,除非erros指定的是’ignore’或者’replace’ |
| bytes.decode(encoding=”utf-8”,errors=”strict”) | python3中的字符串没有decode方法,但是由encode方法返回的bytes对象有decode方法,可以按指定编码格式解码 |
| endswith(suffix,beg=0,end=len(string)) | 检查字符串是否以obj结束,如果beg或者end指定则检查指定的范围内是否以obj结束,如果是,返回True,否则返回False |
| startswith(suffix, beg=0, end=len(string) | 检查字符串是否以obj开头,是则返回True,否则返回False。如果beg和end指定值,则在指定范围内检查 |
| expandtabs(tabsize=8) | 把字符串string中的tab符号转换为空格,tab符号默认的空格数为8 |
| find(str,beg=0,end=len(string)) | 检测str是否包含在字符串中,如果指定范围beg和end,则检查是否包含在指定范围内,如果包含则返回开始的索引值,否则返回-1 |
| index(str, beg=0, end=len(string)) | 同find方法一样,不过如果str不在字符串中会报一个异常 |
| join(seq) | 以指定字符串为分隔符,将seq中所有的元素(的字符串表示)合并为一个新的字符串 |
| ljust(width[,fillchar]) | 返回一个元字符串左对齐,并用fillchar填充至长度width的新字符串, fillchar默认为空格 |
| lower() | 转换字符串中所有大写字符为小写 |
| upper() | 转换字符串中所有小写字符为大写 |
| split(str=””,num=string.count(str)) | 以str为分隔符截取字符串,如果num有指定值,则仅截取num个子字符串 |
| isalnum() | 如果字符串中至少有一个字符,并且所有字符都是字母或数字,则返回True,否则返回False。这在对输入进行检查的时候非常有用 |
| isalpha() | 如果字符串至少有一个字符,并且有所的字符都是字母,则返回True,否则返回False |
| isdigit() | 如果字符串至少有一个字符,并且所有的字符都是数字,则返回True,否则返回False |
| isnumeric() | 如果字符串中至少包含一个字符,并且所有字符都是数字,则返回True,否则返回False。 |
| isdecimal() | 检查字符串中是否只包含十进制字符,这种方法只存在于unicode对象 |
| islower() | 如果字符串中包含至少一个区分大小写的字符,并且所有这些区分大小写的字符都是小写,则返回True,否则返回False |
| isspace() | 如果字符串中只包含空白,则返回True,否则返回False |
| isupper() | 如果字符串中至少至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回True,否则返回False |
| istitle() | 如果字符串是标题化的,则返回True,否则返回False |
举例如下:
# 字符串第一个字母大写s="jimmy doolittle"; print("s=%s" % s.capitalize());# 字符串置中print("%s" % s.center(80, "#"))# 统计字符串中指定字符的个数print("count of mm:%d" % s.count("mm"))print("count of l:%d" % s.count("l"))#指定起始位置和终止位置print("count of m:%d" % s.count("m", 2))print("count of m:%d" % s.count("m", 3))s_example = '我们爱学习'#指定字符编码格式,返回bytes对象s_gbk = s_example.encode('GBK')s_utf8 = s_example.encode('UTF-8')print("GBK:", s_gbk)print("UTF-8:", s_utf8)#对bytes对象按指定格式解码print(s_utf8.decode('UTF-8'))print(s_gbk.decode('gbk'))#判断特定字符串是否在起始位置以及终止位置,或者指定范围的起始位置以及终止位置s_example = '我们爱学习,我们爱科学,我们爱劳动,我们爱学校, 我们爱老师'print("endswith 老师", s_example.endswith('老师', 10))print("endswith 科学", s_example.endswith('科学'))print("startwith 我们", s_example.startswith('我们'))print("startswith 我们 from the 3rd character", s_example.startswith('我们',2))print("startswith 我们 from the 6th character", s_example.startswith('我们',6))#将tab转为空格,默认一个tab八个空格s_tab = "Nothing's gonna change my love for you"s_tab.expandtabs()print("expandtabs=", s_tab)#查找指定字符串的位置,可以指定范围print("find '我们'", s_example.find("我们"))print("index of '爱'", s_example.find('爱'))print("find '我们' between index 3 and 11 is:", s_example.find("我们", 3, 11))#以给定字符为分隔符,合并seq中的字符串s1 = ","seq = ["This","is","a","good","point"]s_join1=s1.join(seq)print("s_join1_no_seperation=", s_join1)#分隔符为空,合并字符串s1 = ""seq = ("The", "Sun", "the", "Moon", "or", "the", "Earth")s_join = s1.join(seq)print("s_join=", s_join)#字符串左对齐,不满指定长度,则右边用指定字符填充s1 = "Hold on a second"s2=s1.ljust(30, "*")print("s_ljust:", s2)
运行结果为:
s=Jimmy doolittle################################jimmy doolittle#################################count of mm:1count of l:2count of m:2count of m:1GBK: b'\xce\xd2\xc3\xc7\xb0\xae\xd1\xa7\xcf\xb0'UTF-8: b'\xe6\x88\x91\xe4\xbb\xac\xe7\x88\xb1\xe5\xad\xa6\xe4\xb9\xa0'我们爱学习我们爱学习endswith 老师 Trueendswith 科学 Falsestartwith 我们 Truestartswith 我们 from the 3rd character Falsestartswith 我们 from the 6th character Trueexpandtabs= Nothing's gonna change my love for youfind '我们' 0index of '爱' 2find '我们' between index 3 and 11 is: 6s_join1_no_seperation= This,is,a,good,points_join= TheSuntheMoonortheEarths_ljust: Hold on a second**************
一个关于使用upper和lower的例子:
confirmation = input("您想去结账吗(y/n)")#利用upper和lower方法对输入字符进行处理,保证无论用户输入的是大写还是小写,都不会有差别if confirmation.upper() == 'Y':print("Paying the bill...")elif confirmation.lower() == 'n':print("Go back for shopping")else:print("Wrong option")
运行结果为:
您想去结账吗(y/n)NGo back for shopping
一个关于startswith和endswith的例子
filelist = ['20180410.log', '20180310.log']# 根据文件起始名称和尾缀来判断是否是需要的文件,假设要统计的是2018年04月的log文件for filename in filelist:if filename.startswith('201804') and filename.endswith('log'):print("{} - that's what I want".format(filename))
运行结果为:
20180410.log - that's what I want
关于split的例子:
single_line = '115.231.99.83 m.example.com [10/Apr/2018:17:58:12 +0800] "GET /404.html HTTP/1.1" "200" 1762 "-" "Apache-HttpClient/4.5.5 (Java/1.8.0_161)" 100.89.115.104 100.217.210.287:443 "-" "0.002" "0.002" "-" "200" "-"'list1=[]#使用split对指定字符串进行分割,默认分隔符为空格,分割为一个序列,可以赋给一个列表list1=single_line.split()print(list1)
结果为:
['115.231.99.83', 'm.example.com', '[10/Apr/2018:17:58:12', '+0800]', '"GET', '/404.html', 'HTTP/1.1"', '"200"', '1762', '"-"', '"Apache-HttpClient/4.5.5', '(Java/1.8.0_161)"', '100.89.115.104', '100.217.210.287:443', '"-"', '"0.002"', '"0.002"', '"-"', '"200"', '"-"']
(五)函数
(1)python3函数
函数是组织好的,可重复使用的,用来实现单一,或相关功能的代码段。
函数能提高应用的模块性,和代码的重复利用率。
(2)定义一个函数
定义函数遵循以下规则:
- 函数代码块以def关键词开头,后接函数标识符名称和圆括号()
- 任何传入参数和自变量必须放在圆括号中间,圆括号之间可以用于定义参数
- 函数内容以冒号起始,并且缩进
- return[表达式]结束函数,选择性地返回一个值给调用方。不带表达式的retrun相当于返回None.
(3)语法
python定义函数使用def关键字,一般格式如下:
def 函数名(参数列表):函数体
(4)调用
函数参数,调用的时候形参和实参个数相同,参数顺序相同
例如:
def x_y_sum_return(x,y):res = x + yreturn res#两个实参,返回一个计算结果z = x_y_sum_return(2,3)print(z)def x_y_comp_list(x,y):res1 = x + yres2 = x * yres_list = [res1,res2]return res_list#将计算结果放在一个列表中返回,返回一个参数print(x_y_comp_list(2,3))def x_y_comp_tuple(x,y):res1 = x + yres2 = x * yres_tuple = (res1,res2)return res_tuple#将计算结果放在一个元组中返回,返回一个参数tu1 = x_y_comp_tuple(100,299)print("tu1=",tu1)def x_y_comp_tuple2(x,y):res1 = x + yres2 = x * yreturn res1,res2#使用两个变量来接收元组返回的两个结果a,b = x_y_comp_tuple2(3,6)print("a=%d,b=%d" % (a,b))
运行结果为:
5[5, 6]tu1= (399, 29900)a=9,b=18
