I have a text file from which I want to extract a certain string based on a variable site which represents the location of some character. I want to extract 20 characters before and after the location of site.
My code works perfectly well if the value of site is over 20. But if it has less than 20 characters before site, it doesn't return anything.
For example, I have a string here where site=5 which in this case is K.
MSGRGKGGKGLGKGGAKRHRKVLRDXYZX
Now I am trying to extract 20 characters before and after the character K.
Below is my code;
data=myfile.read()
str1 = data[site:site+1+20]
temp = data[site-20:site]
final_sequence = temp+str1
print final_sequence
This gives me an output of KGGKGLGKGGAKRHRKVLRDX. Since it couldn't find 20 characters before K, it didn't print the chaarcters before K.
The correct should have been MSGRGKGGKGLGKGGAKRHRKVLRDX.
Which brings me to my question. How can I modify my code to print all characters before K if there are less than 20 characters downstream of the value of K?
Thank you.