The code you read splits a multi-line string into a list, with each line string as a list element.
The code snippet read here is from 30-seconds-of-Python.
split_lines
def split_lines(s) :
return s.split('\n')
# EXAMPLES
split_lines('This\nis a\nmultiline\nstring.\n') # ['This', 'is a', 'multiline', 'string.' , '']
Copy the code
The split_lines function takes a string and splits it into a list, delimited by a newline symbol (\n). The str.split function directly uses the str.split function for string processing. The code in the previous articles used the special use of whitespace string splitting with no arguments. This code specifies line breaking.
str.split(sep=None, maxsplit=-1)
Returns a list of words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits will be done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, there is no limit on the number of splits (all possible splits are made).
If sep is not specified or None, a different splitting algorithm is applied: successive whitespace is treated as a single separator, and the beginning and end, if they contain whitespace, are not split into empty strings. Therefore, splitting an empty string with None or a string containing only Spaces returns [].
>>> '1 2 3'.split()
['1'.'2'.'3']
>>> '1 2 3'.split(maxsplit=1)
['1'.'2, 3,']
>>> '1, 2, 3'.split()
['1'.'2'.'3']
Copy the code