open the file. f.readlines() returns each line in the text file in a list.
Since a list is indexed from 0. list[1:] will skip the first one and give the rest.
with open("stack.txt") as f: txt_list = f.readlines() #That's a list for item in txt_list[1:]: #skip the first one print(item)
output:
2 3 4 5 >>>
And the \n is nothing but a special character denoting a new line. From your input you read multiple lines each having a new line character at the end. Also print prints each one in a new line. That's the reason for (Two newlines) gap between two numbers in above output.
You can join the list and print like this. Since each already has a new line character in it. What you are doing is printing the entire list in your code. yours:
output: ['2\n', '3\n', '4\n', '5']
Try this:
with open("stack.txt") as f: txt_list = f.readlines() out = ''.join(txt_list[1:]) print(out)
output:
2 3 4 5
.read().splitlines(), which will trim the newline characters for you. But better yet, usefor line in file_4andrstrip().file_4variable in two different ways: as the file object and also as a line of the file. The second overwrites the first, so you have no way to do anything more with the file, such as close it. Change it tofor line in lines:or something like that.