Python Glossary Русский

all Python 3Strings (str)function

str.splitlines

splitting

Split the string at line boundaries into a list, without the line breaks.

Syntax

s.splitlines(keepends=False)

It differs from split('\n') in two ways that matter: a trailing newline does not add a stray empty item at the end, and line boundaries include not just \n but \r, \r\n, plus \v, \f, \x1c-\x1e, \x85, \u2028 and \u2029 — text containing those can be cut where you did not expect it. An empty string yields an empty list, not a list with one empty string.

Examples

print('a\nb\nc'.splitlines())  # → ['a', 'b', 'c']
print('a\nb\n'.splitlines())   # → ['a', 'b']
print('one line'.splitlines())  # → ['one line']

See also

Open in the full glossarydocs.python.orgReport an inaccuracy