Split into lines

Python, String · Nov 2, 2020

Splits a multiline string into a list of lines.

  • Use str.split() and '\n' to match line breaks and create a list.
  • str.splitlines() provides similar functionality to this snippet.
def split_lines(s):
  return s.split('\n')
split_lines('This\nis a\nmultiline\nstring.\n')
# ['This', 'is a', 'multiline', 'string.' , '']

More like this

  • String to words

    Converts a given string into a list of words.

    Python, String · Nov 2, 2020

  • Hex to RGB

    Converts a hexadecimal color code to a tuple of integers corresponding to its RGB components.

    Python, String · Sep 15, 2020

  • RGB to hex

    Converts the values of RGB components to a hexadecimal color code.

    Python, String · Nov 2, 2020