简体   繁体   中英

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 + "<-")

Shouldn't I have the same output when printing n and n.strip()?

Shouldn't I have the same output when printing n and n.strip()?

No, you shouldn't have the same output.

Python strip() removes the newlines from the string as they are considered to be whitespace characters.

See Python documentation on str.strip() and on string.whitespace :

string.whitespace A string containing all ASCII characters that are considered whitespace. This includes the characters space, tab, linefeed, return, formfeed, and vertical tab.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM