pythonfunctioncounting

Create a function that prints out the first X nucleotides starting from the Y position


Good morning. I'm trying to figure out how to write code for the following problem

Create a function that prints out the first X nucleotide starting from the Y position (e.g. 5 nucleotides starting from the 3rd nucleotide). X and Y are arguments of the function.

I was given the input values of x and y, but I can't figure out how to get the program to count

X = input([i:i+3])
Y = Input("Input nucleotide" )
def print_segment(X, Y):
    for Y in range(0,len(intput),8):

Solution

  • If you have a DNA strand as a string as suggested in your comments, then you can use python slice notation to get what you seek as strings can behave in many ways like lists. Most importantly, they can be sliced with python slice notation.

    dna_strand = "GATTACA"
    print(dna_strand[2:5])
    

    In the context of asking for input and using a method, perhaps something like:

    def get_segment(strand, start, length=3):
        start = start - 1 # lists are 0 indexed
        return strand[start: start + length]
    
    dna_strand = input("Enter a strand of DNA: ")
    starting_point = int(input("Enter the position of the starting nucleotide: "))
    print(get_segment(dna_strand, starting_point))
    

    If this answers your question, then it is likely a duplicate of:

    How slicing in Python works

    Drop a note here so I can delete this answer and we can mark the question as a duplicate. If this does not answer your question, can you add some examples of actual inputs and outputs so we can help you build a solution to your problem