tnoel999888 tnoel999888 - 2 years ago 106
Python Question

Python Split String On Newline And Keep Newline

Is there a way to split a string containing a newline character into a list of strings where the newline character has been retained, eg.

"amount\nexceeds"


would produce

["amount\n", "exceeds"]

Answer Source

Use splitlines(), passing True:

"amount\nexceeds".splitlines(True)

gives you:

["amount\n", "exceeds"]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download