 Hey everyone, Mr. Gibson here. I'm ready to teach you your next lesson in cryptography. Today, we're going to be covering how to write a function that'll help us format our output for functions. We've learned so far in this course that plaintext and ciphertext get formatted a little bit differently. It's much easier to use Python to format your plaintext, so we know plaintext needs to be lowercase letters. And if we have kind of raw plaintext that we haven't gone through and inserted the spaces to form actual words, then it's just best practice to leave it kind of all lumped together, one big string, no spaces. So we could do that by just using a dot lower method on our output string. And we can, if you want to be nice about it, we can do a dot replace a space with an empty string, make sure there's no spaces in there. And we're done. We can just use two string methods, simple calls to get those done. Formatting our ciphertext is a little bit harder. We know ciphertext needs to be uppercase. That's easy to do. We can just use our dot upper method. But it's also supposed to be blocked into groups of five. And then that's where things get a little bit trickier. So we're going to take a look at today is how can we write a function that'll help block our text out into groups of five characters with a single space in between. No spaces that lead it and no spaces that end it. So I've got to start it here. We've got a function skeleton laid out here with our documentation string. It shows you that you're going to take in a string of text. That's going to be the text that we're planning to block. So it's not going to try and make any predictions about what it needs to do with that in terms of doing to clean it or anything. We're just going to assume that it's good to go. Everything is done to it ahead of time. We just need to block it. So this is all that this code is going to do. Take that string, block it out correctly. The second input argument is the size variable, which we've made a keyword, an optional keyword argument that's defaulting to the integer five. I thought it would be nice in this function that if for whatever reason we wanted to block it into characters of four in a group or seven in a group, that we could do that. But since the standard default is kind of five, we'll just make that our optional keyword argument default of five. I've initialized a string called block text, which is where we're going to move all of the characters that are contained in the string text. As well as some spaces that we're going to introduce at just the right moment. So let's go ahead and iterate over that string text. We've done this before. So we're going to say four character in text. So we're going to initialize the variable character in the four string, and we're going to iterate over the string text. So one character at a time, one substring at a time from the string text is going to be assigned to the variable character. And we're going to do some calculations with that. And what we want to do is figure out when are we going to introduce the space. So we want to do that in this context, every fifth character that we've iterated over. So there's a few ways that we can do this. What I'm going to be looking for is I want to check to see if the total number of characters that have already moved over into the block text string is divisible by five. So I can say if blocked text, mod by five, or in this case, I'm going to use the variable size that way. If I change that to be seven or four, whatever, it's still going to work equals zero. So that's checking to see is it divisible by size. Then I want to add a space before I do anything else. So like if there's already five characters in there, throw a spacing. This is 10 characters in there, throw a spacing. And then after that, after it's decided whether or not it's going to add a space, then I want to just move over whatever character is assigned to the variable character. So again, to kind of go over what the for loop is doing is it's going to pull a character out of the string text. It's going to look at the string block text and decide, is it time for me to introduce a space there or not? If it is, it will. And if it's not, it won't. And then regardless of that decision, it's going to just move that character. It's going to copy it over into the block text string. It's going to do that for every single letter in text. Now we're going to see that this code actually will not work, not at least the way that we intended to. So let's just take a look and see what this will do and see how we might be able to fix it. Okay, so let's take our kind of standard. Hello, world string, and let's see what it does here in a perfect world. This is going to work. We're going to have hello as one block and world as a second block. But let's see what happens. We can see that that is not happening. And there's a few reasons for that. The first is you can see that there's a space before the age is kind of hard to tell because you can't really see spaces. But when you highlight it, you can see that it is in fact highlighting a space here. So it's introducing a space right off the bat, which is not what we want. But then it looks like it's counting the number of spaces when it's deciding is the length of the message divisible by five. And that's not really what we want to do. We want to kind of how many characters that aren't spaces are in that string and see if that quantity is divisible by five. So let's go back and see if we can fix that problem first. Let's go back and see instead of counting or counting of the length of the block text string as is, let's take out the spaces and replace them with an empty string. Remember, when we use the dot replace, that's not actually modifying the string block text, it's just going to pull up that string temporarily make that replacement for whatever operation we're currently doing. But it doesn't actually permanently change that string. So we're not we're not going to mess anything up here. And now what it's going to check to see is if it takes out all the spaces or whatever is left over divisible by five. And that sounds more like what we were intending to do. We don't want to count spaces and deciding is it time for me to introduce a new space. So let's rerun that code and see what that does. This is looking a little better. I don't think this is a little bit longer. Hello, world. Mr. Gibson is here. And we can see, yeah, that that's looking pretty good is blocking everything out in groups of five until it runs out of characters to use. And then the last block is for that looks perfect. It still has that leading space in front, though. And that might not seem like a big deal. But again, a lot of what we're trying to accomplish with our code is predictable inputs that give us predictable outputs. And if you're about to pass off this block text to something else and it's not expecting a space there, that might cause some issues. So you want to think about why is that happening? Let's go back up to our code. The very first time we enter the for loop, it's going to check is the length of the block text without any spaces in it divisible by whatever we set the size. So by default five. And that's going to be true the very first time we run it because that the very first time you run it blocks text has a length of zero. And that is divisible by five or seven or whatever number you put the size. It's always divisible by it. So the very first thing that it's going to do that block text string is introduce a space right off the bat. That's where this space is coming into play. So we can get around that. I think the easiest way to get around that is just introduce a second condition in our if statement. We don't want it to do that if it's the length of the string is zero. Every other time that seems to be working fine. But we want to just make sure that if the length of the block text without spaces is divisible by our size variable and the length of the block text is not equal to zero. Then go ahead and add the space. So about both of these things to be true before we introduce a space. And since this highlighted one here, the length of the block text not equal to zero is not true the first time we run this. It won't introduce this space when we first start out and there's nothing currently in the block text string. So I'm going to rerun this. And now we can see all right is blocking things out correctly. There's no leading space that I can select over here. This is the way we want our output to be formatted. So that should do it here for our text block function. I'll just really quick verify that the optional keyword argument does work if I want to make for. I can block it in groups of four if I want to block it in groups of seven. That all works. We've got a lot of functionality here on this short little function. So I would encourage you to add this to your toolkit as well. So you've got this readily available whenever you need it. This will be helpful whenever we're creating ciphertext that we need to space out the correct way. Thank you for watching and we'll catch you on the next one.