Skipping a line or a sentence or output has always remain a part of programming since ages. But programmers are all not aware of the different ways of doing it in output or while writing to files.
In this chapter, programmers will get detailed information on how to skip a line in python. Programmers can also learn about the 'use file.readlines()' and slicing. You can refer to the examples below to gain a better understanding.
How to Skip a Line in Python?
There are many ways in which you can skip a line in python. Some methods are:
if, continue, break, pass, readlines(), and slicing.
Using 'if' statement
The primary purpose of the 'if' statement is to control the direction of programs. Sometimes, you get certain results you might not want to execute. In those cases, we use the 'if' statement to skip the execution. It is a naive method and is illogical.
Code:
num = [1, 2, 3, 4]
for i in num:
if i==3:
print()
print(i)
Output:
Using Continue statement.
We use the 'continue' statement to skip the execution of the current iteration of the loop. To avoid error, we do not use this statement outside of it.
Code:
for val in "string":
if val == "i":
continue
print(val)
print("The end")
Output:
Using the 'break' statement
It ends the current loop and performs execution at the following statement. We can use this statement in both 'while' and the 'for' loop.
Code:
count = 10
while count > 0:
print(count)
if count == 5:
break
count -= 1
Output:
Using Pass statement
When we don't want to execute any command or code, and when the statement is required syntactically, we use this statement.
Code:
s = "Gaurav"
for i in s:
pass
def fun():
pass
fun()
for i in s:
if i == 'v':
print('Pass executed')
pass
print(i)
Output:
Using readlines() method
The primary function of the readlines() method is to read a file and then return a list. Since this function returns the list, we can repeat it. If the line number you are currently on is equal to the line number you wish to skip, you remove that line. If not, you consider it.
In the example below, we print all the lines except the one we want to skip.
Code:
def skipLine(f, skip):
lines = f.readlines()
skip = skip - 1
for line_no, line in enumerate(lines):
if line_no==skip:
pass
else:
print(line, end="")
Output:
We can skip the first line and write the same program as follows:
Program:
try:
f = open("sample.txt", "r")
skipLine(f, 1)
finally:
f.close()
Output:
The readlines() method is very efficient, and we use it generally. You can even use the readlines() along with list slicing. Slicing does not skip a line. But when we use it with list slicing, it does the job. You can get an explanation on Slicing and List Slicing below.
Using Slicing concept
We use this method to create a substring from a given string. When we have to slice a sequence, a slice object helps. It also helps to identify where to start and end a slicing. It generally takes three parameters:
- Start
- Stop
- Step
Step parameter helps to enumerate the steps required from start to end index.
Syntax:
sliceobject = slice(start, stop, step)
List Slicing
As we have already noticed, the readlines() method returns a list. It is the reason why we can use slicing to skip a line.
Code:
def skipLineSlicing(f, skip):
skip -= 1
if skip < 0:
skip= 1
lines = f.readlines()
lines = lines[0:skip] + lines[skip+1:len(lines)]
for line in lines:
print(line, end="")
Output:
We can also write this code by skipping the last line. It is a sample.txt file.
Code:
try:
f = open("sample.txt", "r")
skipLineSlicing(f, 5)
finally:
f.close()
Output:
Conclusion:
Here we have learned some of the best ways to skip a line. One of the best methods is the readlines() method for files and for skipping any specific code output or create a gap, pass or if-print() combination are the best and the most efficient ones. Skipping lines or output also helps in symmetrically design or print output in many apps and pattern-based systems.
Skipping a line or a result also works in software like duplicate finder, duplicate checker, plagiarism tools, etc. However, there are other methods as well that we can use.
List slicing is one of the best methods because it can bring customization is slicing but less efficient as it requires more variables and objects which increase space complexity.