Python String splitlines()
Python String splitlines() function returns the list of lines in the string. The line boundaries are not included in the string, unless we provide keepends
argument value as True
.
Python String splitlines()
The following table shows the characters that are treated as line boundary by splitlines() function.
Character | Description |
---|---|
n | Line Feed |
r | Carriage Return |
rn | Carriage Return + Line Feed |
v or x0b | Line Tabulation (Python 3.2 onwards) |
f or x0c | Form Feed (Python 3.2 onwards) |
x1c | File Separator |
x1d | Group Separator |
x1e | Record Separator |
x85 | Next Line (C1 Control Code) |
u2028 | Line Separator |
u2029 | Paragraph Separator |
Python String split into lines
Let’s look into some examples of splitlines() function.
s="JavanPythonnAndroid"
lines = s.splitlines()
print(lines)
lines = s.splitlines(keepends=True)
print(lines)
Output:
['Java', 'Python', 'Android']
['Javan', 'Pythonn', 'Android']
Let’s look at another example with a combination of line feed and carriage return characters.
s="JavannnnPythonnrnAndroid" # rn is treated as a single line boundary
lines = s.splitlines()
print(lines)
lines = s.splitlines(keepends=True)
print(lines)
Output:
['Java', '', '', '', 'Python', '', 'Android']
['Javan', 'n', 'n', 'n', 'Pythonn', 'rn', 'Android']
If you get confused with the output from splitlines() function, test it with keepends
as True
to understand how the split is performed.
Finally, let’s look into some examples with different separator characters.
s="AtBvCfD"
lines = s.splitlines()
print(lines)
lines = s.splitlines(keepends=True)
print(lines)
s="Ax1cBx1dCx1eDx85Eu2028Fu2029G"
lines = s.splitlines()
print(lines)
lines = s.splitlines(keepends=True)
print(lines)
Output:
['AtB', 'C', 'D']
['AtBx0b', 'Cx0c', 'D']
['A', 'B', 'C', 'D', 'E', 'F', 'G']
['Ax1c', 'Bx1d', 'Cx1e', 'Dx85', 'Eu2028', 'Fu2029', 'G']
Python String split() vs splitlines()
- We can specify separator in split() function, splitlines() is only meant to split string into list of lines.
- There are slight difference in the split() and splitlines() function working. Let’s see that with examples:
print("".split('n')) print("".splitlines()) print("Hin".split('n')) print("Hin".splitlines())
Output:
[''] [] ['Hi', ''] ['Hi']
So with line break as separator, split() returns empty string in the list whereas splitlines() returns empty list.
Official Documentation: partition()