Now let's write a few functions to manipulate DNA strings. We're going to start by writing a function to find the longest common prefix between two strings. So, to create a function in Python, you want to start by writing def, and then your function name, so I'm going to call it longest common prefix. And then I'm going to pass in as arguments the two strings, s1 and s2. Now, if you're new to Python, indentation is very important, so inside this function, everything needs to be indented, or I'm going to get an error when I try to run it. So now the way this function is going to work is I'm going to create an index. I'm going to start from zero, and as long as this index is within the range of both strings, I'm going to see whether the two strings are identical and if so, I'm going to increment the index. So I'm going to use a while loop. I'm going to say, while i is less than the length of both strings, and the character index i and s1 is equal to the character index i and s2. As long as this is the case, I am going to increment i. >> So, when we finish this loop, I'll be at the point where either we've exhausted one or both of the strings or we've reached a mismatch. So, now we can just return the part of either string, up to but not including i. And this will give us the longest common prefix of the two strings. Let's test this. So I'll just make up two strings here. I'll give them both the prefix, ACCA and then they'll be different. So when I run this, I get a longest common prefix of ACCA. So that function works correctly. Now let's do something similar, except in this case let's write a function to see whether two strings match exactly. So I'm going to call this function match. And the first thing I can do is I can say, if the two strings are not of the same length, they definitely can't be a match. So if the length of s1 is not equal to the length of s2, I can immediately return false. Now if not, I need to loop through the strings and make sure that all the characters are the same, so I'm going to use a for loop. I'm going to say for i In range up to the length of the strings. If they don't have the same character here, then the strings don't match so I return false. Now if I finish this loop and I haven't returned, that must mean every character matches between the two strings. So I can just return true. So let's test this out. I'm going to match two strings. I'll start by giving it the same string to make sure it works when they're the same. And that returns true, like we want. Let me try it now with two strings that are not the same, and that returns false. So that method works correctly. What I didn't tell you is Python actually already has a method to do this for us. We can just use a double equal sign. So I can say, this string equals equals and then give it another string, Those are not the same and so this returns false. But if I give it two of the same string, that returns true. So, we don't actually need a method to do this, we can just use the built in method in Python. Now let's write a function to get the reverse complement of a DNA strand. In order to do this, we're going to use a new Python structure called a dictionary. All right, so I'm going to create a dictionary called complement. I define it with curly braces instead of square brackets, and a dictionary is basically just an associative array. So I'm going to associate each DNA base with its complementary base. So the key, so I get a key such as A and then the value that it's associated with, so T, and then I'm going to do this for each base. Just like that. And so now, if I look for key A in this dictionary, it outputs T. If I look for key C that outputs G and so on for each of the keys that I've defined. So we're going to use a dictionary like this in our function, which I'm going to call reverse complement. Press in a string s, and I start by defining this complement dictionary. And then I create a string t, which will be the reverse complement. I'm going to start with an empty string. And then for each base in s, I'm going to take t, and I'm going to set t to be the complement of the base in s plus t, then I return t. Notice that I'm adding the complement of each base to the beginning of t rather than to the end of it. This causes it to reverse the string since we're going from the back to the front instead of from beginning to end. >> So the complement dictionary is what helps us complement the bases, and then the prepending is what makes us reverse the string. >> That's right. Yep. >> Okay. >> So now let's test this out. Let's pass in a random string. I just showed a string that's it's own reverse complement. Let me change it a little bit. So there you can see, if you look at the beginning of this, it's a C. That's the reverse complement of G. Then G is the complement of C, and so on through the whole string. So we just reverse complemented the string. >> So if we had double stranded DNA, and we knew the sequence along one of those strands from top to bottom, this would give us the sequence of the other strand from bottom to top. >> Right. Yeah.