 <p>If you slice a string with a <em>negative</em> number, Python counts backward from the end.</p> <pre><code>complement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'} s = "AAAAAAAAAACCCCCCCCCCTTTTTTTTTTGGGGGGGGGG" "".join(complement[c] for c in s[-20:-10]) </code></pre> <hr> <p>EDIT:</p> <p>Your edit looks about right to me, yes. I am very bad at checking for <a href="http://en.wikipedia.org/wiki/Off-by-one_error" rel="nofollow">fencepost errors</a>, but you're better placed to see those than I am anyway!</p> <p>I've rewritten your code to be more Pythonic, but not changed anything substantial.</p> <pre><code>bc = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A', 'N':'N'} f = open('chr22.fa') l = ''.join(line.strip() for line in f) f.seek(0) for line in f: fields = line.split('\t') gene_ID, chr, strand = fields[:2] start = int(fields[3]) end = int(fields[4]) if strand == '-': start, end = -(start + 1), -(end + 1) geneseq = ''.join(bc[base.upper()] for base in l[start:end]) </code></pre>