廖雪峰Python教程疑问

1. 字符串的strip()方法

原文:

“把一个序列中的空字符串删掉,可以这么写:

def not_empty(s):

   return s and s.strip()

list(filter(not_empty, ['A', '', 'B', None, 'C', ' ']))

# 结果: ['A', 'B', 'C']”

疑问:如果改为,

def not_empty(s):

   return s.strip()

就会提示错误,AttributeError: ‘Nonetype’ object has no attribute ‘strip’

不知道 s and s.strip() 与 s.strip()的区别在哪里。

出处:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431821084171d2e0f22e7cc24305ae03aa0214d0ef29000

 

    原文作者:Abraham_Xu
    原文地址: http://www.cnblogs.com/blue-box/p/5805439.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞