1 #字符串的所有操作
2
3 name = 'Lief \tis a {type} student.'
4 print(name.center(39,'-')) # 将字符串放置在另一个重复字符串的中间
5 print(name.endswith("f")) # 判断是否以某个指定字符串结尾
6 print(name.expandtabs(tabsize = 9)) # 在\t处加空格
7 print(name[name.find('student'):]) #
8 print(name[name.find('student')])
9 print(name.format(type='college')) # 填入内容
10 print('+'.join(['q','w','e','r'])) # 在字符间加入单个字符
11 print(name.rjust(30,'L')) # 在字符串左边加上若个单个字符
12 print(name.ljust(30,'f')) # 在字符串右边加上若个单个字符
13 print(name.lower()) # 变成小写
14 print(name.upper()) # 变成大写
15 print(' \nLief'.lstrip()) # 去掉空格、换行
16 print('Lief\n'.rstrip())
17 print('\nLief\n'.strip())
18 # 造密码!!!!!
19 Transfer = str.maketrans('qwertyuiopasdfghjklzxcvbnm','12345678901234567890123456')
20 print('hzwwddl'.translate(Transfer))
21
22 print(name.replace('e', 'Q',)) # 替换字符串中的某个字符
23
24 print(name.rfind('s')) # 找到最靠右的那个字符的下标
25
26 print(name.partition('st')) # 在出现指定字符的地方将整串字符拆分
27 print(name.split('st')) # 在出现指定字符的地方将整串字符拆分,并删除指定字符,输出的是列表,如下
28
29 a = name.split('st')
30 print(a[1])
31
32 print('my name\nis lee'.splitlines()) # 按换行将字符串拆分成列表里的元素
33
34 print(name.swapcase()) # 转换大小写
1 # 字典的多级查找
2
3 catalog = {
4 'TJU': {'A': {'re'}, 'D': {'wr'}, 'R': {'we'}},
5 'NKU': {'W': {'a': {'we'}, 's': {'re'}, 'd': {'wy'}}},
6 'PKU': {'U': {'wddd'}, 'P': {'wgg'}},
7 'TZU': {'I': {'wefse'}, 'X': {'fhusde'}}
8 }
9 exit_flag = False
10 while not exit_flag:
11 for i in catalog:
12 print(i) # 打印第一层
13 choice = input('Input your choice you want in,input q to quit: ') # 输入选择
14 if choice in catalog:
15 while not exit_flag:
16 for i2 in catalog[choice]: # 在里面就打印第二层
17 print('\t', i2)
18 choice2 = input('Input your choice2 you want in,input q to quit: ') # 输入选择
19 if choice2 in catalog[choice]:
20 while not exit_flag:
21 for i3 in catalog[choice][choice2]: # 在里面就打印第三层
22 print('\t\t',i3)
23 choice3 = input('Input your choice3 you want in,input q to quit: ') # 输入选择
24 if choice3 in catalog[choice][choice2]:
25 while not exit_flag:
26 for i4 in catalog[choice][choice2][choice3]:
27 print('\t\t\t', i4)
28 print('You have reached the bottom.')
29 exit_flag = True
30 elif choice3 == 'q':
31 exit_flag = True
32 elif choice3 == 'b':
33 break
34 else:
35 print('Does not exist or you have reached the bottom.')
36 elif choice2 == 'q':
37 exit_flag = True
38 elif choice2 == 'b':
39 break
40 else:
41 print('Does not exist or you have reached the bottom.')
42 elif choice == 'q':
43 exit_flag = True
44 elif choice == 'b':
45 break
46 else:
47 print('Does not exist or you have reached the bottom.')
1 # eval 能将字符串转成字典
2
3 dic = eval(
4 """{
5 'TJU': {'A': [34, 21], 'D': [21,22], 'R': [39,321]},
6 'NKU': {'W': {1: [29,12], 2: [34,22], 3: [23,66]}},
7 'PKU': {'U': [67,32], 'P': [33,88]},
8 'TZU': {'I': [88,54], 'X': [55,437]}
9 }""")
10
11 print(dic['TJU'])