• 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 Count Number of Lines in String

Using Python to Count Number of Lines in String

July 11, 2022 Leave a Comment

To count the number of lines in a string, the easiest way is to use split() and split the line with the newline character. Then use len()

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

count = len(string.split("\n"))

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

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

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

import re

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

count = len(re.split("\n", string))

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

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

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 string and want to know how many lines your string contains.

You can find out how many lines your string contains by splitting the string by the newline character and using the len() function to get the count of items returned by split().

Below is a simple example showing you how to get the number of lines in a string using Python.

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

count = len(string.split("\n"))

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

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

Hopefully this article has been useful for you to learn how to get the number of lines in a string using Python.

Other Articles You'll Also Like:

  • 1.  Remove First and Last Character from String Using Python
  • 2.  Python divmod() Function – Get Quotient and Remainder After Division
  • 3.  Get Random Value from Dictionary in Python
  • 4.  Count Unique Values in pandas DataFrame
  • 5.  How to Measure Execution Time of Program in Python
  • 6.  Drop First Row of pandas DataFrame
  • 7.  Split Column by Delimiter in pandas DataFrame
  • 8.  Python cos – Find Cosine of Number in Radians Using math.cos()
  • 9.  Add Tuple to List in Python
  • 10.  pandas cumprod – Find Cumulative Product of Series or DataFrame

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