English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The splitlines() method splits the string at the newline characters and returns a list of lines in the string.
The syntax of splitlines() is:
str.splitlines([keepends])
splitlines() can contain at most1Parameter.
keepends (Optional)-If keepends is provided and is True, then the newline characters are also included in the items of the list.
By default, it does not contain newline characters.
splitlines() returns a list of lines in the string.
If there is no newline character, it returns a list containing a single item (a single line).
splitlines() splits at the following line boundaries:
Means | Description |
---|---|
\n | New line |
\r | Carriage return |
\r\n | Carriage return+New line |
\v Or \x0b | Line tab |
\f Or \x0c | Form feed |
\x1c | File separator |
\x1d | Field separator |
\x1e | Record separator |
\x85 | Next line (C1Specify code) |
\u2028 | Line separator |
\u2029 | Paragraph separator |
grocery = 'Milk Chicken Bread Butter' print(grocery.splitlines()) print(grocery.splitlines(True)) grocery = 'Milk Chicken Bread Butter' print(grocery.splitlines())
When running the program, the output is:
['Milk', 'Chicken', 'Bread', 'Butter'] ['Milk', 'Chicken', 'Bread', 'Butter'] ['Milk Chicken Bread Butter']