一、string方法
字符串相加
name = 'chenkangle'
print(name.__add__('love'))
字符串包含
name = 'chenkangle'
print(name.__contains__('ka'))
字符串相等
name = 'chenkangle'
print(name.__eq__('chenkangl'))
print(name.__eq__('chenkangle'))
字符串大于等于
name = 'chenkangle'
print(name.__ge__('chenkan'))
字符串首字母大写
name = 'chenkangle'
print(name.capitalize())
字符串全部小写
name = 'ChenkAngle'
print(name.casefold())
等号为边界分隔,宽度为20个字符
name = 'ChenkAngle'
print(name.center(20,'='))
统计字符串出现的次数
name = 'today is a very good day07!'
print(name.count('da'))
字符串以什么开头
name = 'today is a very good day07!'
print(name.startswith('to'))
print(name.startswith('i',7,15))
字符串以什么结尾
name = 'today is a very good day07!'
print(name.endswith('ay'))
去掉首位的空格
name = ' today is a very good day07! '
print(name.strip())
替换出现的子串为新串,后面的数字为替换几次,默认为-1,表示都替换
name = 'wahahaishao'
print(name.replace('ha','HA',2))
查找子串在给定的区域内首次出现的位置,从0开始
name = 'woxianghayihaishao'
print(name.index('ha',10,15))
同index作用基本一致,找字符串出现的位置,如果没有找到,返回-1,而index会报错
name = 'woxianghayihaishao'
print(name.find('hm'))
切割字符串,以给定的参数为切割符来切割,返回一个列表
name = 'woxianghayihaishao'
print(name.split('ha'))
将tab转为空格,默认是8个,可以指定
name = 'wo\thao'
print(name.expandtabs())
print(name.expandtabs(2))
format功能类似于%s
name = "today {0} is {1} fine {2}"
result = name.format('really','a','day07')
print(result)
变量的处理
name = "your {name} and your {id}"
result = name.format(name='ckl',id='893')
print(result)
序列拼接,按照指定的分隔符进行拼接
ckllist = ['c','h','u','n','t','i','a','n']
result = "-".join(ckllist)
print(result)
判断字符串内容是否为数字
num = '123'
print(num.isdigit())
#结果为true,即内容为数字
1 passwd = input('please input your pass: ').strip() 2 if len(passwd) > 0: 3 if passwd.isdigit(): 4 passwd=input(passwd) 5 print(passwd) 6 else: 7 print("passwd must be a number type.") 8 else: 9 print("passwd is not empty")
二、集合
1 s1 = {22,87,39,5,10,15}
2 s2 = {17,15,39,22,10}
3
4 #交集
5 print(s1 & s2)
6 print(s1.intersection(s2))
7
8 #并集
9 print(s1 | s2)
10 print(s1.union(s2))
11
12 #差集
13 print(s1 - s2)
14 print(s1.difference(s2))
15 print(s2 - s1)
16 print(s2.difference(s1))
17
18 #对称差集
19 print(s1 ^ s2)
20 print(s1.symmetric_difference(s2))
21
22 #集合添加值,集合内的值必须是不可变类型,也就是列表,字典均不可
23 s1.add(3)
24 s1.add(9)
25 print(s1)
26
27 #集合无序的,弹出内容也是无序的
28 p1 = s1.pop()
29 p2 = s1.pop()
30 print(p1)
31 print(p2)
32 print(s1)
33
34 #集合制定删除
35 s1.remove(9) #如果值不存在会报错
36 #s1.remove(112)
37 print(s1)
38
39 #这样的方法,不存在就不会报错
40 s1.discard(112)
41 s1.discard(10)
42 print(s1)
结果:
{10, 15, 22, 39}
{10, 15, 22, 39}
{5, 39, 10, 15, 17, 22, 87}
{5, 39, 10, 15, 17, 22, 87}
{5, 87}
{5, 87}
{17}
{17}
{17, 5, 87}
{17, 5, 87}
{3, 5, 39, 9, 10, 15, 22, 87}
3
5
{39, 9, 10, 15, 22, 87}
{39, 10, 15, 22, 87}
{39, 15, 22, 87}
三、练习超市购物程序
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 import sys
4 ShopCart = []
5 Wallet = [30000,0]
6
7 #定义商品字典包含编号、名称、价格
8 GoodsBuyDict = {
9 '1':{
10 'ipad':3000
11 },
12 '2':{
13 'mac':21000
14 },
15 '3':{
16 'car':29000
17 },
18 '4':{
19 'bag':1800
20 },
21 '5':{
22 'tv':2800
23 }
24 }
25
26 #定义一个打印商品名称的方法
27 def PrintGoods():
28 print("goods list".center(32, '='))
29 for Gnum, Gdic in GoodsBuyDict.items():
30 for Goods, Price in Gdic.items():
31 print('%s No is %-5s price is :\t%s' % (Gnum, Goods, Price))
32
33 #定义一个打印购物车清单的方法
34 def PrintGcart():
35 print("goods cart have".center(32, '*'))
36 for Gdname in ShopCart:
37 print(Gdname)
38
39 Gbool = True
40 while Gbool:
41 PrintGoods()
42 #循环输入需要购买的商品编号
43 InputNum = input("Please input you wanna buy goods Number: ")
44
45 #定义还能购买的商品列表,初始为空
46 CanBuyDict = {}
47
48 #定义一个方法来收集已经购买的商品并增加到CanBuyDict字典里
49 def CanBuyFunc():
50 for GoodsPrice in GoodsBuyDict.values():
51 for Gname, Gprice in GoodsPrice.items():
52 if Wallet[0] > Gprice:
53 CanBuyDict[Gname] = Gprice
54
55 #如果输入的序列号存在就开始操作
56 if InputNum in GoodsBuyDict.keys():
57 for Gk,Gp in GoodsBuyDict[InputNum].items():
58 if int(Wallet[0]) > Gp:
59 #购物车增加商品
60 ShopCart.append(Gk)
61 #钱包余额剩余和化肥
62 Wallet[0] -= Gp
63 Wallet[1] += Gp
64 else:
65 #执行还能购买商品的函数,并收集能购买的字典内容
66 CanBuyFunc()
67 print("You can not pay for this good,and your credit is running low!")
68 print("")
69 print("You can buy list")
70 print("="*40)
71 #打印还能购买商品列表
72 for Cd,Cp in CanBuyDict.items():
73 print("%s price is %s" %(Cd,Cp))
74 print("Your pay money is %d and left %d" % (Wallet[1], Wallet[0]))
75 #执行还能购买商品的函数,并收集能购买的字典内容
76 CanBuyFunc()
77 #如果列表为空,说明不能购买任何商品了,就退出程序
78 if CanBuyDict.__len__() < 1:
79 print("Sorry, you can't buy anything !")
80 print("")
81 PrintGcart()
82 sys.exit(1)
83 else:
84 #输入的序号不在,则继续循环
85 print("Sorry,You must choose a number in the list !")
86 continue
87 #关于是否继续购买的循环
88 while True:
89 print("")
90 ContinueBuy = input("Do you want continue to by y or n: ")
91 if ContinueBuy == 'y':
92 break
93 elif ContinueBuy == 'n':
94 #如果退出就打印购物车清单并设定循环值为False
95 PrintGcart()
96 Gbool = False
97 break
98 else:
99 print("You Must choos y or n !")