美文网首页
strip()去掉什么

strip()去掉什么

作者: 是人也 | 来源:发表于2019-01-24 10:04 被阅读0次

Documentation:

Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:
The outermost leading and trailing chars argument values are stripped from the string. Characters are removed from the leading end until reaching a string character that is not contained in the set of characters in chars. A similar action takes place on the trailing end.

关于line.strip()的用法:用来去掉文字首尾的whitespace, 如果没有文字,那么去掉whitespace

The most common whitespace characters may be typed via the space bar or the tab key. Depending on context, a line-break generated by the return or enter key may be considered whitespace as well.

这段话说明whitespace包括空格,Tab和换行符

line1: [' 1\n', '2 4\n', '\n', '3 \n', '5\n']
line2: ['1', '2 4', '', '3', '5']

line1中的元素.strip()后变成line2, 所以如果是空行,strip()后会成为[‘’], 任要判断len(line)来去掉

相关文章

网友评论

      本文标题:strip()去掉什么

      本文链接:https://www.haomeiwen.com/subject/hbxyjqtx.html