![](/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.