fh=open('asd.txt')
data=fh.read()
fh.close()
name=data.split('\n')[0][1:]
seq=''.join(data.split('\n')[1:])
print name
print seq
In this code, the 3rd line means “take only first line with first character removed” while the 4th line means “leave the first line and join the next remaining lines”.
I cannot get the logic of these two lines.
Can anyone explain me how these two slice operators ([0][1:]) are used together?
Thanx
Edited: renamed file variable (which is a keyword, too) to data.
Think of it like this:
file.split('\n')gives you a list of strings. So the first indexing operation,[0], gives you the first string in the list. Now, that string itself is a “list” of characters, so you can then do[1:]to get every character after the first. It’s just like starting with a two-dimensional list (a list of lists) and indexing it twice.