Split into Lines | Challenge

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Python, a multiline string is a string that contains multiple lines of text. Sometimes, it is necessary to split a multiline string into a list of individual lines. This can be useful when you need to process each line separately.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") subgraph Lab Skills python/comments -.-> lab-13189{{"`Split into Lines | Challenge`"}} python/lists -.-> lab-13189{{"`Split into Lines | Challenge`"}} python/function_definition -.-> lab-13189{{"`Split into Lines | Challenge`"}} python/standard_libraries -.-> lab-13189{{"`Split into Lines | Challenge`"}} end

Split into Lines

Problem

Write a function called split_lines(s) that takes a multiline string s as input and returns a list of individual lines. Your function should split the string at each line break (\n) and return a list of the resulting lines.

Example

split_lines('This\nis a\nmultiline\nstring.\n')
## ['This', 'is a', 'multiline', 'string.' , '']

Summary

To split a multiline string into a list of individual lines in Python, you can use the split() method with the line break character (\n) as the delimiter. This will split the string at each line break and return a list of the resulting lines.

Other Python Tutorials you may like