count a newline in string python 1

count a newline in string python

length = x.count('\n')

Here is what the above code is Doing:
1. We open the file and read it into a variable called x.
2. We split the file into a list of lines using the splitlines() method.
3. We count the number of lines in the list using the count() method.
4. We assign the number of lines to the variable line_length.

Similar Posts