How to calculate each string length that belongs to a list of strings by python?
<p>Suppose I have a file with <code>n</code> DNA sequences, each one in a line. I need to turn them into a list and then calculate each sequence's length and then total length of all of them together. I am not sure how to do that before they are into a list. </p>

<pre><code># open file and writing each sequences' length
f= open('seq.txt' , 'r')
for line in f:
    line= line.strip()
    print (line)
    print ('this is the length of the given sequence', len(line))

# turning into a list:  
lines = [line.strip() for line in open('seq.txt')]
print (lines)
</code></pre>

<p>How can I do math calculations from the list? Ex. the total length of all sequences together? Standard deviation from their different lengths etc.</p>

<p>This will do what you require. To do additional calculations you may want to save your results from the text file into a list or set so you won't need to read from a file again.</p>

<pre><code>total_length = 0  # Create a variable that will save our total length of lines read

with open('filename.txt', 'r') as f:
    for line in f:
        line = line.strip()
        total_length += len(line)  # Add the length to our total
        print("Line Length: {}".format(len(line)))

print("Total Length: {}".format(total_length))
</code></pre>

python string list python-3.x math