• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Split String by Newline

Using Python to Split String by Newline

June 1, 2022 Leave a Comment

To split a string by newline in Python, you can use the Python string split() function and pass ‘\n’ to get a list of strings.

string = "This is a\nstring with\nnewline in it"

print(string.split("\n"))

#Output:
["This is a", "string with", "newline in it"]

You can also use the split() function from the re (regular expression) module.

import re

string = "This is a\nstring with\nnewline in it"

print(re.split("\n", string))

#Output:
["This is a", "string with", "newline in it"]

When working with strings and text in Python, the ability to manipulate and create new objects from strings can be useful.

One such situation is if you have newline characters in your strings and want to get the substrings between the newline characters.

To split a string by newline in Python, you can use the Python string split() function and pass ‘\n’ to get a list of strings.

Below is a simple example showing you how you can use split() to split a string by newline into a list of strings.

string = "This is a\nstring with\nnewline in it"

print(string.split("\n"))

#Output:
["This is a", "string with", "newline in it"]

Splitting String by Newline with re.split() Function in Python

Another way you can split a string by the newline character is to use the regular expression module split() function to perform a regular expression which will find the “\n” characters and then create a list of strings.

Below is a simple example showing you how you can use re.split() to split a string by newline into a list of strings in Python.

import re

string = "This is a\nstring with\nnewline in it"

print(re.split("\n", string))

#Output:
["This is a", "string with", "newline in it"]

Splitting String When There are More than One Newline in Python

Many times, you have more than one lines which you want to get rid of or deal with. With the re module, you can pass ‘\n+’ to re.split() and split a string which has multiple newline characters.

Below is a simple example showing you how to split a string with multiple newline characters.

import re

string = "This is a\n\nstring with\n\n\n\nnewline in it"

print(re.split("\n+", string))

#Output:
["This is a", "string with", "newline in it"]

Hopefully this article has been useful for you to learn how to split a string by newline in Python.

Other Articles You'll Also Like:

  • 1.  math.radians() python – How to Convert Degrees to Radians in Python
  • 2.  Using Python to Print Environment Variables
  • 3.  How to Draw a Triangle in Python Using turtle Module
  • 4.  Python tan – Find Tangent of Number in Radians Using math.tan()
  • 5.  Convert String to Integer with int() in Python
  • 6.  Get First Character of String in Python
  • 7.  Get Length of File Using Python
  • 8.  Convert String to Boolean Value in Python
  • 9.  Writing Multiple Lines Lambda Expression in Python
  • 10.  Get Elapsed Time in Seconds in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy