![](/img/trans.png)
[英]Why is there an extra new line character when I create a string using init(contentsOfFile:)?
[英]Why this happen when I strip a string starting or ending with new line character?
n = "\n1\n2\n3\n4\n \n"
print("->" + n.strip() + "<-")
print("->" + n + "<-")
打印 n 和 n.strip() 时,我不应该有相同的 output 吗?
打印 n 和 n.strip() 时,我不应该有相同的 output 吗?
不,你不应该有相同的 output。
Python strip()
从字符串中删除换行符,因为它们被认为是空白字符。
请参阅有关str.strip()
和 string.whitespace 的string.whitespace
文档:
string.whitespace
包含所有被视为空白的 ASCII 字符的字符串。 这包括字符空格、制表符、换行符、回车符、换页符和垂直制表符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.