NumPy Splitlines Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will cover how to use the splitlines() function in the char module of the NumPy library. This function splits the strings present in an array into substrings based on line breaks present in the strings.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/with_statement -.-> lab-86504{{"`NumPy Splitlines Function`"}} python/booleans -.-> lab-86504{{"`NumPy Splitlines Function`"}} python/for_loops -.-> lab-86504{{"`NumPy Splitlines Function`"}} python/tuples -.-> lab-86504{{"`NumPy Splitlines Function`"}} python/importing_modules -.-> lab-86504{{"`NumPy Splitlines Function`"}} python/numerical_computing -.-> lab-86504{{"`NumPy Splitlines Function`"}} python/build_in_functions -.-> lab-86504{{"`NumPy Splitlines Function`"}} end

Import the Required Libraries

We begin by importing the NumPy library. Run the following code to import the NumPy library:

import numpy as np

Using the splitlines() Function Without Keepends Parameter

In this step, we will use the splitlines() function to separate the lines of a string present in an array. After applying the splitlines() function, we will get a list of the lines in the string, breaking at line boundaries.

string1 = "Codecademy\noffers\ncourses\nin\nprogramming."
out = np.char.splitlines(string1)
print("After applying splitlines() function:")
print(out)

Output:

After applying splitlines() function:
['Codecademy', 'offers', 'courses', 'in', 'programming.']

Using the splitlines() Function With Keepends Parameter

In this step, we will see how to use the keepends parameter in the splitlines() function. If we want to include line breaks, we can set the value of keepends parameter as True.

string2 = "Python is a programming language\nused for various applications."
out = np.char.splitlines(string2, keepends=True)
print("After applying splitlines() function with keepends parameter:")
print(out)

Output:

After applying splitlines() function with keepends parameter:
['Python is a programming language\n', 'used for various applications.']

Using the splitlines() Function with Multiple Lines in a String

In this step, we will use the splitlines() function on a string that contains multiple lines.

string3 = """Python is a high-level programming language
with a wide range of applications,
such as web development, data science, machine learning, and AI."""
out = np.char.splitlines(string3)
print("After applying splitlines() function:")
print(out)

Output:

After applying splitlines() function:
['Python is a high-level programming language', 'with a wide range of applications,', 'such as web development, data science, machine learning, and AI.']

Summary

In this lab, we learned about the splitlines() function of the char module in the NumPy library. We used the splitlines() function to separate the lines in a string present in an array by line boundaries. We also saw how to use keepends parameter to include line breaks in the output.