对字符串的操作
#String 内容
a = "Let's go"
乘法操作:
print('hellow'*2)
通过索引获得字符串中字符,这里和列表的切片操作是相同的,具体内容见列表:
print('hellow woeld'[2:])从位置2取到最后
成员运算符操作,字符中包含给定字符则返回True:
print('e2l' in 'hello') 存在返回True 不在返回False
例如:
print(123 in [12,123,1111])
结果:True
格式化字符串:
print('alex is a good teacher')
print('%s is a good teacher'%'alex')
字符串的拼接:
a='123'
b='456'
c = a+b
print(c)
结果:
123456
拼接:join方法
a='123'
b='abc'
c='000'
d = '#####'.join([a,b,c])
结果:
123####abc####000
python内置方法:
1 #_auther_="stuwu79"
2 #date:2019/9/27
3
4 st = 'hello kitty {name} is {age}'
5 st.count("l")#计算'l'个数
6 st.capitalize()#整个字符串首字母大写
7 st.center(50,'-')#(居中)打印50个字符,本身字符串在中间,剩下的两边补上'-'
8 st.rjust(50,"*")
9 st.ljust(50,"*")#只在左,其他补齐*
10
11 st.encode()#编码问题
12
13
14 st.endswith("y")#判断以什么结尾。正确给True,错误给False
15 st.startswith("h")#判断以什么开头
16 st.expandtabs(tabsize=20)#在字符串中间放\t设定插入空格数
17
18 st.find('l')#查找第一个元素在字符串的位置并且返回(空格也算位置)
19 st.index('t')#找不到就报错(给 -1)
20
21 st.format(name = 'alex',age = '37')#格式化输出的方式
22 st.format_map({'name':'alex','age':22})#意义相同,方式不同
23
24 'abc456'.isalnum() #不是特殊字符都给True
25 'AF00'.isdecimal() #判断是不是10进制
26
27 '12121212'.isdigit() #判断是不是整型
28 '121233'.isnumeric() #一样的
29
30 '1121324'.isidentifier()#判断是不是非法变量(根据变量命名规则)
31
32 'Abc'.islower()#判断是否是全小写(全大写)
33 'Abc'.isupper()
34
35 ' e'.isspace()#判断有没有空格
36 'adasda'.istitle()#判断是不是标题(每个单词首字母大写)
37 'adasdad'.lower()#所有大写变小写
38 'adasda'.upper()
39 'adasda'.swapcase()#翻转,大写变小写。小写变大写
40
41 ' asda\n'.strip()#前后的空格,换行符全去掉
42 ' asda\n'.lstrip()#只去除左边的
43 ' asda\n'.rstrip()#只去除右边的
44
45 'My title title'.replace('itle','lesson',1)#替换,某些内容替换成另一个内容可以控制替换几次
46 'My title title'.rfind('t')#从右向左找
47 'My title title'.split(' ')#以“ ”为分割对象对字符串进行分割(将字符串分割成列表)
48 'My title title'.title() #每个单词首字母大
49
50
51 #特别重要的字符串方法:
52
53
54 st.count("l")#计算'l'个数
55 st.startswith("h")#判断以什么开头
56 st.find('l')#查找第一个元素在字符串的位置并且返回(空格也算位置)
57
58
59 st.format(name = 'alex',age = '37')#格式化输出的方式
60 st.format_map({'name':'alex','age':22})#意义相同,方式不同
61
62
63 ' asda\n'.strip()#前后的空格,换行符全去掉
64 ' asda\n'.lstrip()#只去除左边的
65 ' asda\n'.rstrip()#只去除右边的
66
67 'My title title'.split(' ')#以“ ”为分割对象对字符串进行分割(将字符串分割成列表)
68
69 'My title title'.replace('itle','lesson',1)#替换,某些内容替换成另一个内容可以控制替换几次
三级菜单的实现:
本次实现重复代码较多:
1 #_auther_="stuwu79"
2 #date:2019/9/27
3 #要求:可以进入所有层
4 #可以退出或者返回上一层
5 menu = {
6 '北京':{
7 '朝阳':{
8 '国贸':{
9 'CICC':{},
10 'HP':{},
11 'CCTV':{},
12 },
13 '望京':{
14 '陌陌':{},
15 '奔驰':{},
16 '360':{},
17 },
18 '三里屯':{
19 '优衣库':{},
20 'apple':{},
21 },
22 },
23 '昌平':{
24 '沙河':{
25 '老男孩':{},
26 '阿泰包子':{},
27 },
28 '天通苑':{
29 '链家':{},
30 '我爱我家':{},
31 },
32 '回龙观':{},
33 },
34 '海淀':{
35 '五道口':{
36 "谷歌":{},
37 "网易":{},
38 "Sohu":{},
39 "快手":{},
40 },
41 '中关村': {
42 "youku": {},
43 "iqiyi": {},
44 "汽车之家": {},
45 "新东方": {},
46 "QQ": {},
47 },
48 },
49 },
50 '上海':{
51 '浦东':{
52 "陆家嘴":{
53 "CICC":{},
54 "高盛":{},
55 "摩根":{},
56 },
57 "外滩": {},
58 },
59 '闵行':{},
60 '静安':{},
61 },
62 '山东':{
63 "济南":{},
64 "德州":{
65 "乐陵":{
66 "丁务镇":{},
67 "居然镇":{},
68 },
69 "平原":{},
70 },
71 "青岛":{},
72 },
73 }
74
75 back_flag = False
76 exit_flag = False
77
78 while not back_flag and not exit_flag:
79 for key in menu :
80 print(key)
81 choice = input("1>>:").strip()
82 if choice == "q":
83 exit_flag = True
84 if choice in menu:
85 while not back_flag and not exit_flag:#让程序停在第二层
86 for key2 in menu[choice]:
87 print(key2)
88 choice2 = input("2>>:").strip()
89 if choice2 == "b":
90 back_flag = True
91 if choice2 == "q":
92 exit_flag = True
93 if choice2 in menu[choice]:
94 while not back_flag and not exit_flag:
95 for key3 in menu[choice][choice2]:
96 print(key3)
97 choice3 = input("3>>:").strip()
98 if choice3 == "b":
99 back_flag = True
100 if choice3 == "q":
101 exit_flag = True
102 if choice3 in menu[choice][choice2]:
103 while not back_flag and not exit_flag:
104 for key4 in menu[choice][choice2][choice3]:
105 print(key4)
106 choice4 = input("4>>:").strip()
107 print("last level")
108 if choice4 =="b":
109 back_flag = True
110 if choice4 == "q":
111 exit_flag = True
112 else:
113 back_flag = False
114 else:
115 back_flag = False
116
117 else:
118 back_flag = False
修改版本:
#_auther_="stuwu79"#date:2019/9/27#要求:可以进入所有层#可以退出或者返回上一层menu = { '北京':{ '朝阳':{ '国贸':{ 'CICC':{}, 'HP':{}, 'CCTV':{}, }, '望京':{ '陌陌':{}, '奔驰':{}, '360':{}, }, '三里屯':{ '优衣库':{}, 'apple':{}, }, }, '昌平':{ '沙河':{ '老男孩':{}, '阿泰包子':{}, }, '天通苑':{ '链家':{}, '我爱我家':{}, }, '回龙观':{}, }, '海淀':{ '五道口':{ "谷歌":{}, "网易":{}, "Sohu":{}, "快手":{}, }, '中关村': { "youku": {}, "iqiyi": {}, "汽车之家": {}, "新东方": {}, "QQ": {}, }, }, }, '上海':{ '浦东':{ "陆家嘴":{ "CICC":{}, "高盛":{}, "摩根":{}, }, "外滩": {}, }, '闵行':{}, '静安':{}, }, '山东':{ "济南":{}, "德州":{ "乐陵":{ "丁务镇":{}, "居然镇":{}, }, "平原":{}, }, "青岛":{}, },}back_flag = Falseexit_flag = Falsewhile not back_flag and not exit_flag: for key in menu : print(key) choice = input("1>>:").strip() if choice == "q": exit_flag = True if choice in menu: while not back_flag and not exit_flag:#让程序停在第二层for key2 in menu[choice]: print(key2) choice2 = input("2>>:").strip() if choice2 == "b": back_flag = True if choice2 == "q": exit_flag = True if choice2 in menu[choice]: while not back_flag and not exit_flag: for key3 in menu[choice][choice2]: print(key3) choice3 = input("3>>:").strip() if choice3 == "b": back_flag = True if choice3 == "q": exit_flag = True if choice3 in menu[choice][choice2]: while not back_flag and not exit_flag: for key4 in menu[choice][choice2][choice3]: print(key4) choice4 = input("4>>:").strip() print("last level") if choice4 =="b": back_flag = True if choice4 == "q": exit_flag = True else: back_flag = False else: back_flag = False else: back_flag = False