tnoel999888
tnoel999888

Reputation: 654

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"]

Upvotes: 15

Views: 6035

Answers (1)

Stuart
Stuart

Reputation: 6785

Use splitlines(), passing keepends=True:

"amount\nexceeds".splitlines(keepends=True)

gives you:

["amount\n", "exceeds"]

Upvotes: 25

Related Questions