DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

splitlines in Python

Buy Me a Coffee

*Memo for string, bytes and bytearray functions:

*Memo for a string, bytes and bytearray:

str.splitlines() and bytes.splitlines() or bytearray.splitlines() can split a string and bytes or bytearray respectively at one or more line boundaries as shown below:

*Memo:

  • The 1st argument is keepends(Optional-Default:False-Type:bool):
    • If keepends is True, one or more line boundaries are included otherwise they aren't included.
  • These below are line boundaries:
\n Line Feed
\r Carriage Return
\r\n Carriage Return + Line Feed
\v or \x0b Line Tabulation
\f or \x0c Form Feed
\x1c File Separator
\x1d Group Separator
\x1e Record Separator
\x85 Next Line (C1 Control Code)
\u2028 Line Separator
\u2029 Paragraph Separator

<String>:

v1 = '1 one\n2 two\r3 three\r\n4 four\v5 five'
v2 = '1 one\x0b2 two\f3 three\x0c4 four'
v3 = '1 one\x1c2 two\x1d3 three\x1e4 four'
v4 = '1 one\x852 two\u20283 three\u20294 four'

print(v1)
# 1 one
# 3 three
# 4 four5 five

print(v1.splitlines())
print(v1.splitlines(keepends=False))
# ['1 one', '2 two', '3 three', '4 four', '5 five']

print(v1.splitlines(keepends=True))
# ['1 one\n', '2 two\r', '3 three\r\n', '4 four\x0b', '5 five']

print(v2)
# 1 one2 two3 three4 four

print(v2.splitlines())
# ['1 one', '2 two', '3 three', '4 four']

print(v2.splitlines(keepends=True))
# ['1 one\x0b', '2 two\x0c', '3 three\x0c', '4 four']

print(v3)
# 1 one2 two3 three4 four

print(v3.splitlines())
# ['1 one', '2 two', '3 three', '4 four']

print(v3.splitlines(keepends=True))
# ['1 one\x1c', '2 two\x1d', '3 three\x1e', '4 four']

print(v4)
# 1 one…2 two
3 three
4 four

print(v4.splitlines())
# ['1 one', '2 two', '3 three', '4 four']

print(v4.splitlines(keepends=True))
# ['1 one\x85', '2 two\u2028', '3 three\u2029', '4 four']
Enter fullscreen mode Exit fullscreen mode
v = '1 one 2 two 3 three 4 four 5 five'

print(v)
# 1 one 2 two 3 three 4 four 5 five

print(v.splitlines())
print(v.splitlines(keepends=True))
# ['1 one 2 two 3 three 4 four 5 five']
Enter fullscreen mode Exit fullscreen mode
v = ''

print(v.splitlines())
# []
Enter fullscreen mode Exit fullscreen mode

<Bytes & Bytearray>:

bytes:

v1 = b'1 one\n2 two\r3 three\r\n4 four\x0b5 five'
v2 = b'1 one\x0b2 two\x0c3 three\x0c4 four'
v3 = b'1 one\x1c2 two\x1d3 three\x1e4 four'
v4 = b'1 one\xc2\x852 two\xe2\x80\xa83 three\xe2\x80\xa94 four'

print(v1)
# b'1 one\n2 two\r3 three\r\n4 four\x0b5 five'

print(v1.splitlines())
print(v1.splitlines(keepends=False))
# [b'1 one', b'2 two', b'3 three', b'4 four\x0b5 five']

print(v1.splitlines(keepends=True))
# [b'1 one\n', b'2 two\r', b'3 three\r\n', b'4 four\x0b5 five']

print(v2)
# b'1 one\x0b2 two\x0c3 three\x0c4 four'

print(v2.splitlines())
print(v2.splitlines(keepends=True))
# [b'1 one\x0b2 two\x0c3 three\x0c4 four']

print(v3)
# b'1 one\x1c2 two\x1d3 three\x1e4 four'

print(v3.splitlines())
print(v3.splitlines(keepends=True))
# [b'1 one\x1c2 two\x1d3 three\x1e4 four']

print(v4)
# b'1 one\xc2\x852 two\xe2\x80\xa83 three\xe2\x80\xa94 four'

print(v4.splitlines())
print(v4.splitlines(keepends=True))
# [b'1 one\xc2\x852 two\xe2\x80\xa83 three\xe2\x80\xa94 four']
Enter fullscreen mode Exit fullscreen mode
v = b'1 one 2 two 3 three 4 four 5 five'

print(v)
# b'1 one 2 two 3 three 4 four 5 five'

print(v.splitlines())
print(v.splitlines(keepends=True))
# [b'1 one 2 two 3 three 4 four 5 five']
Enter fullscreen mode Exit fullscreen mode
v = b''

print(v.splitlines())
# []
Enter fullscreen mode Exit fullscreen mode

bytearray:

v1 = bytearray(b'1 one\n2 two\r3 three\r\n4 four\v5 five')
v2 = bytearray(b'1 one\x0b2 two\f3 three\x0c4 four')
v3 = bytearray(b'1 one\x1c2 two\x1d3 three\x1e4 four')
v4 = bytearray(b'1 one\x852 two\u20283 three\u20294 four')

print(v1)
# bytearray(b'1 one\n2 two\r3 three\r\n4 four\x0b5 five')

print(v1.splitlines())
print(v1.splitlines(keepends=False))
# [bytearray(b'1 one'), bytearray(b'2 two'), bytearray(b'3 three'),
#  bytearray(b'4 four\x0b5 five')]

print(v1.splitlines(keepends=True))
# [bytearray(b'1 one\n'), bytearray(b'2 two\r'),
#  bytearray(b'3 three\r\n'), bytearray(b'4 four\x0b5 five')]

print(v2)
# bytearray(b'1 one\x0b2 two\x0c3 three\x0c4 four')

print(v2.splitlines())
print(v2.splitlines(keepends=True))
# [bytearray(b'1 one\x0b2 two\x0c3 three\x0c4 four')]

print(v3)
# bytearray(b'1 one\x1c2 two\x1d3 three\x1e4 four')

print(v3.splitlines())
print(v3.splitlines(keepends=True))
# [bytearray(b'1 one\x1c2 two\x1d3 three\x1e4 four')]

print(v4)
# bytearray(b'1 one\xc2\x852 two\xe2\x80\xa83 three\xe2\x80\xa94 four')

print(v4.splitlines())
print(v4.splitlines(keepends=True))
# [bytearray(b'1 one\xc2\x852 two\xe2\x80\xa83 three\xe2\x80\xa94 four')]
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'1 one 2 two 3 three 4 four 5 five')

print(v)
# bytearray(b'1 one 2 two 3 three 4 four 5 five')

print(v.splitlines())
print(v.splitlines(keepends=True))
# [bytearray(b'1 one 2 two 3 three 4 four 5 five')]
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'')

print(v.splitlines())
# []
Enter fullscreen mode Exit fullscreen mode

Top comments (0)