Python String splitlines() Method

Example

Split the string into a list where each line is an item in the list:

txt = "Thank you for your visiting\nWelcome to China"
x = txt.splitlines()
print(x)

Run Instance

Definition and Usage

The splitlines() method splits a string into a list. The split is done at newline characters.

Syntax

string.splitlines(keeplinebreaks)

Parameter Value

Parameter Description
keeplinebreaks Optional. Specifies whether to include newline characters (True) or not (False). The default value does not include (False).

More Examples

Example

Split strings but keep newline characters:

txt = "Thank you for your visiting\nWelcome to China"
x = txt.splitlines(True)
print(x)

Run Instance