-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdo_slice.py
More file actions
40 lines (33 loc) · 895 Bytes
/
Copy pathdo_slice.py
File metadata and controls
40 lines (33 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack'] # 索引从0开始
print('L[0:3]=', L[0:3])
print('L[:3]=', L[:3])
print('L[1:3]=', L[1:3])
print('L[-2:]=', L[-2:])
R = list(range(100))
print('R[:10]=', R[:10])
print('R[-10]=', R[-10])
print('R[10:20] =', R[10:20])
print('R[:10:2] =', R[:10:2])
print('R[::5] =', R[::5])
def trim(s): # slice参数
while s[:1] == ' ':
s = s[1:]
while s[-1:] == ' ':
s = s[:-1]
return s
if trim('hello ') != 'hello':
print('测试失败!')
elif trim(' hello') != 'hello':
print('测试失败!')
elif trim(' hello ') != 'hello':
print('测试失败!')
elif trim(' hello world ') != 'hello world':
print('测试失败!')
elif trim('') != '':
print('测试失败!')
elif trim(' ') != '':
print('测试失败!')
else:
print('测试成功!')