为什么查找以"imooc"结尾的语句需要加"\n" line.endswith("imooc\n")?
endswith() 方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。
\n 换行符,相当于转义,为什么不加的话就找不到匹配项呢?是否所有用到endswith()方法的语句在条件结尾都要加上"\n"?
谢谢。
def find_imooc(fname):
f = open(fname)
for line in f:
if line.startswith('imooc') and line.endswith("imooc\n"):
print line
find_imooc("test")