name =" egon "print(name.strip())print(name)
name ="** *eg*on***"print(name.strip("*"))
x ="*(-)=+abc=-)*/"print(x.strip("*()-=+/"))
name =input("username>>>: ").strip()
pwd =input("password>>>: ").strip()if name =="egon"and pwd =="123":print("ok")else:print('error')
2.3.6 切分split
info ="root:123:0:0"
res = info.split(":",1)print(res[0])print(res)
2.3.7 循环
msg ="hello"for i in msg:print(i)
2.4 字符串类型需要掌握的操作
#1、strip, lstrip, rstrip 去除左右字符(空格、符号等)print(" xxx ".strip())print("*******xxx******".strip('*'))print(" xxx ".lstrip())print(" xxx ".rstrip())# 2、lower, upperprint("HeLlo".upper())print("HeLlo".lower())# 3、startswith,endswith 判断是否以什么开头和结尾print("wushu666".startswith('wu'))print("wushu666".startswith('shu'))# 4、format的三种玩法print("My name is {xxx} and my age is {yyy}.".format(yyy=18, xxx="wu"))print("My name is {} and my age is {}.".format(18,"wu"))print("My name is {0}{0}{0} and my age is {1}{0}.".format(18,"wu"))# format 高级玩法# 对齐print("My name is {0:*<10} and my age is {1:(<12}.".format(18,"wu"))print("My name is {0:*>10} and my age is {1:(>12}.".format(18,"wu"))print("My name is {0:*^10} and my age is {1:)^12}.".format(18,"wu"))# 进制与精度print("My name is {0:b} and my age is {1}.".format(18,"wu"))print("My name is {0:o} and my age is {1}.".format(18,"wu"))print("My name is {0:x} and my age is {1}.".format(18,"wu"))print("My name is {0:,} and my age is {1}.".format(18381231803,"wu"))print("My name is {0:.3f} and my age is {1}.".format(188.23132148,"wu"))# f python新版本可以用
name ="wu"
age =18print(f"My name is {name} and my age is {age}.")# 字典
info ={"name":"wu","age":18}print("My name is {name} and my age is {age}.".format(**info))# 列表
l =[111,222]print("My name is {} and my age is {}.".format(*l))# 5、split,rsplit 分割(空格、字符等)
info ="root:a:c:b:11:31"
l1 = info.split(":")print(l1)# 6、join 连接print(":".join(l1))# 7、replace 替换# 8、isdigit 判断字符串是否由纯数字组成print('222'.isdigit())
age =input("your age:").strip()if age.isdigit():
age =int(age)if age >18:print("too big")elif age <18:print("too small")else:print("you got it")else:print("必须输入数字!")
#1、 按key存取值:可存可取
d ={'k1':111,'k2':222,'k3':333}print(d['k1'])print(d.keys())
d['k1']=666
d['k4']=444print(d)# 2、长度lenprint(len(d))# 3、成员运算in和not inprint('k1'in d)# 4、删除# del d['k1']# print(d)
res = d.pop('k2')print(res)print(d)# 5、键keys(),值values(),键值对items()# 6、循环for k, v in d.items():print(k, v)# 7、getprint(d.get('kkkk'))# 需要掌握的操作
dic = d.copy()print(dic)
d.update({'k5':5555,'k4':2441})print(d)
res = d.pop('k4')print(res)print(d)
res = d.setdefault("k1",6666666666)print(d)print(res)
5.4 总结
存多个值、无序、可变
d ={'k1':[],'k2':[]}print(id(d['k1']))print(id(d['k2']))
d ={}.fromkeys(['k1','k2'],[])print(id(d['k1']))print(id(d['k2']))
nums =[11,22,33,44,55,66,77,88,99,1111]for num in nums:if num >66:
d['k1'].append(num)else:
d['k2'].append(num)print(d)
s ='hello alex hello alex say sb sb'
words = s.split()
d ={}for word in words:if word notin d:
d[word]=1else:
d[word]+=1print(d)
六、集合类型
6.1 用途
去重(鸡肋)、关系运算(并集交集差集)
6.2 定义
在{}内用多个逗号分开放多个不可变类型
# 特点:# 元素不可变、元素无序,元素唯一
s ={111,333,'sss',(11,34,'ff')}print(s,type(s))# 自己写代码去重(集合set去重鸡肋)
l =[{'name':'egon','age':18,'sex':'male'},{'name':'alex','age':73,'sex':'male'},{'name':'egon','age':20,'sex':'female'},{'name':'egon','age':18,'sex':'male'},{'name':'egon','age':18,'sex':'male'},]
res =[]for item in l:# print(item)if item notin res:
res.append(item)print(res)