<p>When the user is pushing <code>enter</code> on their keyboard, it is likely inserting a newline character (<code>\n</code>) at the end of the user input <code>entry</code> and causing the error you have included in your question. You can strip the <code>\n</code> by using <code>entry.rstrip('\n')</code>:</p>
<pre class="lang-py prettyprint-override"><code># entry - user inputted string taking in amino acid letters
entry = e1.get()
entry = entry.rstrip('\n')
prot_data = ProteinAnalysis(entry)
result = prot_data.instability_index()
</code></pre>
<hr />
<p>From the comments where the <code>print(entry)</code> was given as</p>
<blockquote>
<p><code>MALWMRLLPLLALLALWGPDPAAAFVNQHLCGSHLVEALYLVCGERGFFYTPKTRREAEDLQVGQVELGG GPGAGSLQPLALEGSLQKRGIVEQCCTSICSLYQLENYCN</code></p>
</blockquote>
<p>and <code>print(repr(entry))</code> is given as</p>
<blockquote>
<p><code>'MALWMRLLPLLALLALWGPDPAAAFVNQHLCGSHLVEALYLVCGERGFFYTPKTRREAEDLQVGQVELGG\nGPGAGSLQPLALEGSLQKRGIVEQCCTSICSLYQLENYCN'</code></p>
</blockquote>
<p>the <code>\n</code> character is not at the end of the line so <code>.rstrip('\n')</code> cannot be used (it only works at the end of the string).</p>
<p>Try instead <code>.replace('\n', '')</code>:</p>
<pre class="lang-py prettyprint-override"><code># entry - user inputted string taking in amino acid letters
entry = e1.get()
entry = entry.replace('\n', '')
prot_data = ProteinAnalysis(entry)
result = prot_data.instability_index()
</code></pre>
<p>which on my system (removing the <code>prot_data</code> and <code>result</code> lines) outputs without the <code>\n</code> character and <code>print(repr(entry))</code> is the same as <code>print(entry)</code>.</p>

CC BY-SA 4.0
2023-06-20T00:59:57.533Z
@Z_Gonzalez rightttt, it's in the middle of the string, I just assumed as it was user input the `\n` was at the end, `.rstrip()` only works from the end of the string, we need to use `.replace()` instead, will edit the answer shortly
piJT
CC BY-SA 4.0
2023-06-19T12:05:50.020Z
2023-06-20T16:16:41.647Z
2023-06-20T16:16:41.647Z
piJT
<p>I am trying to run a function from the ProtParam script in the Bio.SeqUtils package. The function is for calculating the instability index of a protein. The code I am using in my script with the pre-built function is the following:</p>
<pre><code># entry - user inputted string taking in amino acid letters
entry = e1.get()
prot_data = ProteinAnalysis(entry)
result = prot_data.instability_index()
</code></pre>
<p>When I run the code, I get the following key error related to the instability_index function.</p>
<pre><code>File &quot;/Genome_Toolkit/venv/lib/python3.9/site- packages/Bio/SeqUtils/ProtParam.py&quot;, line 152, in instability_index
dipeptide_value = index[this][next]
KeyError: '\n'
</code></pre>
<p>The instability_index() function in the ProtParam script looks like this:</p>
<pre><code>def instability_index(self):
    index = ProtParamData.DIWV
    score = 0.0

    for i in range(self.length - 1):
        this, next = self.sequence[i: i + 2]
        dipeptide_value = index[this][next]
        score += dipeptide_value

    return (10.0 / self.length) * score
</code></pre>
<p>I looked at the script myself, specifically the line the error is referring to but couldn't pinpoint the issue. Any guidance is appreciated. Thanks!</p>
<p>the output of <code>print(entry)</code> and <code>print(repr(entry))</code> looks like this, which is exactly what the entry was in the input:</p>
<pre><code>MALWMRLLPLLALLALWGPDPAAAFVNQHLCGSHLVEALYLVCGERGFFYTPKTRREAEDLQVGQVELGG
GPGAGSLQPLALEGSLQKRGIVEQCCTSICSLYQLENYCN
'MALWMRLLPLLALLALWGPDPAAAFVNQHLCGSHLVEALYLVCGERGFFYTPKTRREAEDLQVGQVELGG\nGPGAGSLQPLALEGSLQKRGIVEQCCTSICSLYQLENYCN'
</code></pre>

<proteins><errors><biopython>
KeyError '/n' in Bio SeqUtils Package ProtParam Script
# KeyError '/n' in Bio SeqUtils Package ProtParam Script

(Asked by: username_0 on 2023-06-18)

I am trying to run a function from the ProtParam script in the Bio.SeqUtils package. The function is for calculating the instability index of a protein. The code I am using in my script with the pre-built function is the following:
# entry - user inputted string taking in amino acid letters
entry = e1.get()
prot_data = ProteinAnalysis(entry)
result = prot_data.instability_index()

When I run the code, I get the following key error related to the instability_index function.
File "/Genome_Toolkit/venv/lib/python3.9/site- packages/Bio/SeqUtils/ProtParam.py", line 152, in instability_index
dipeptide_value = index[this][next]
KeyError: '\n'

The instability_index() function in the ProtParam script looks like this:
def instability_index(self):
    index = ProtParamData.DIWV
    score = 0.0

    for i in range(self.length - 1):
        this, next = self.sequence[i: i + 2]
        dipeptide_value = index[this][next]
        score += dipeptide_value

    return (10.0 / self.length) * score

I looked at the script myself, specifically the line the error is referring to but couldn't pinpoint the issue. Any guidance is appreciated. Thanks!
the output of print(entry) and print(repr(entry)) looks like this, which is exactly what the entry was in the input:
MALWMRLLPLLALLALWGPDPAAAFVNQHLCGSHLVEALYLVCGERGFFYTPKTRREAEDLQVGQVELGG
GPGAGSLQPLALEGSLQKRGIVEQCCTSICSLYQLENYCN
'MALWMRLLPLLALLALWGPDPAAAFVNQHLCGSHLVEALYLVCGERGFFYTPKTRREAEDLQVGQVELGG\nGPGAGSLQPLALEGSLQKRGIVEQCCTSICSLYQLENYCN'

**Tags:** proteins, errors, biopython

---

## 1 answers

### Answer by username_1 on 2023-06-19. Score: 3 | Accepted answer

When the user is pushing enter on their keyboard, it is likely inserting a newline character (\n) at the end of the user input entry and causing the error you have included in your question. You can strip the \n by using entry.rstrip('\n'):
# entry - user inputted string taking in amino acid letters
entry = e1.get()
entry = entry.rstrip('\n')
prot_data = ProteinAnalysis(entry)
result = prot_data.instability_index()


From the comments where the print(entry) was given as

MALWMRLLPLLALLALWGPDPAAAFVNQHLCGSHLVEALYLVCGERGFFYTPKTRREAEDLQVGQVELGG GPGAGSLQPLALEGSLQKRGIVEQCCTSICSLYQLENYCN

and print(repr(entry)) is given as

'MALWMRLLPLLALLALWGPDPAAAFVNQHLCGSHLVEALYLVCGERGFFYTPKTRREAEDLQVGQVELGG\nGPGAGSLQPLALEGSLQKRGIVEQCCTSICSLYQLENYCN'

the \n character is not at the end of the line so .rstrip('\n') cannot be used (it only works at the end of the string).
Try instead .replace('\n', ''):
# entry - user inputted string taking in amino acid letters
entry = e1.get()
entry = entry.replace('\n', '')
prot_data = ProteinAnalysis(entry)
result = prot_data.instability_index()

which on my system (removing the prot_data and result lines) outputs without the \n character and print(repr(entry)) is the same as print(entry).

> **Comments:**
> **username_1**: @Z_Gonzalez rightttt, it's in the middle of the string, I just assumed as it was user input the `\n` was at the end, `.rstrip()` only works from the end of the string, we need to use `.replace()` instead, will edit the answer shortly  

---

