#切片:
# L = ['aaa', 'bbb', 'ccc', 'ddd']
# print(L[1 : 3]) #取[1, 3):下标
# L = list(range(100))
# print(L[:10])
# print(L[-10:])
#
# print(L[2:10:4]) #从2开始取,步长为4,小于10
# # [2, 6]
# print('abcdefg'[:3])
# # abc
# print('abcdefg'[::2])
# # aceg #取出字符串的首位空格的函数
# def trim(s):
# if s == '':
# return ''
# len1 = len(s)
# i = 0
# j = len1 - 1
# while i < len1 and s[i] == ' ':
# i += 1
# print(i)
# while j > 0 and s[j] == ' ' :
# j -= 1
# print(j)
# if i <= j:
# print(i, (j))
# return s[i:j+1]
# else:
# return '' #递归写法:
# def trim(s):
# if s == '':
# return s
# if s[:1] == ' ':
# # if s[0] == ' ':
# return trim(s[1:])
# if s[-1:] == ' ':
# # if s[-1] == ' ':
# return trim(s[:-2])
# else:
# return s
#递归写法2:
def trim(s):
if s == '':
return s
if s[0] == ' ':
s = trim(s[1: ]) #如果这里不用返回值的话,必须要在下面判空,否则可能s已经为空了,再去做下一个if判断,就会越界
# return trim(s[1: ])
if s == '':
return s
if s[-1] == ' ':
s = trim(s[ :-2])
# return trim(s[ :-2])
return s
#
# # 测试:
t = [1, 2, 3, 4, 5]
print(t[:-2]) if trim(' ') != '':
print('ys' + trim(' hello') + 'ys')
print('no')
#
if trim('hello ') != 'hello':
print('a' + trim('hello ') + 'b')
print('1测试失败!')
elif trim(' hello') != 'hello':
print('2测试失败!')
elif trim(' hello ') != 'hello':
print('3测试失败!')
elif trim(' hello world ') != 'hello world':
print('4测试失败!')
elif trim('') != '':
print('5测试失败!')
elif trim(' ') != '':
print('6测试失败!')
else:
print('测试成功!')

  

最新文章

  1. Oracle临时文件
  2. 日本DARTS 支撑的一系列应用项目
  3. CSS3-box盒布局
  4. Memcached【Magent+Memcached】集群
  5. zoj 3644(dp + 记忆化搜索)
  6. ed编辑器使用
  7. Git一张图学习
  8. DHCP Option 60 的理解
  9. 可视化查看MongoDB - MongoVUE
  10. python3、selenium、autoit3,通过flash控件上传文件
  11. winzip15.0注冊码
  12. 斗地主 (NOIP2015 Day1 T3)
  13. Visual Studio 2012 开发环境配置+控制台工具+桌面应用程序
  14. webservice05#soap消息
  15. hdu 4468 spy 极其精彩的一道kmp灵活运用题
  16. js验证是否为数字的总结(转)
  17. Java基础:JVM垃圾回收算法
  18. yarn依赖管理工具,和fis3构建工具 gulp详细用法
  19. dfs模版
  20. Android百度地图2.0运行定位到当前位置时“服务没有启动”

热门文章

  1. ActiveMQ面试题
  2. win7下破解无线网密码
  3. kafka系列之(3)——Coordinator与offset管理和Consumer Rebalance
  4. 51nod 1085 背包问题
  5. 转载 fpga中 restoring 和 non-restoring 除法实现。
  6. w3cschool在线教程
  7. Ctrl+H 浪潮Raid配置文档
  8. ffmpeg摄像头采集h264编码RTP发送
  9. Java-Maven-Runoob:Maven IntelliJ
  10. Java-Maven-Runoob:Maven NetBeans