 In this lesson, we'll learn how strings work in Python in order to help you work with text. And then we'll see how we can use these newfound knowledge to help us implement the rail fence cipher using Python. Strings are how we're going to work with text and other characters, such as numbers and punctuation, while we're working in Python. We saw in an earlier lesson that strings are denoted using these single quotes or double quotes on either side of the text. And you can assign them to variable names. Here we have the string Harry, which is lowercase h a r r y within the single quotes that are assigned to the variable named first name. And then we can use the print function either on a string directly itself or on the variable that contains a string in order to print it out to the screen. On the left hand side, we did that with a string that actually contains some characters. We'll also be using what's known as the empty string on the right hand side of our screen. We have assigned to the variable named first name, single quote, single quote, nothing in between. We call that the empty string is technically a string, even though it contains no characters, it would have a length of zero. What we'll go on to see now is that string data types can also be used with some specific operators or operations. And they also have some built-in functions that are called methods that'll provide some extra functionality to our strings. First off, we can concatenate two strings together. Concatenate is just kind of a fancy word for combined end to end. And we can do that using the addition symbol that we'd seen in a previous lesson for adding two numbers together. So here we have the variable first name assigned to the string Harry Potter, and then the string Potter assigned to the variable named last name. And we're going to print first name plus last name or first name concatenated with the last name. Now notice when we do that print statement, those two strings are glued end to end. There's no spacing in between them. Here in our second example, we'll do the same thing with our first name and last name being Taylor and Swift respectively. But we'll create a third variable called full name where we concatenate first name, a string that contains a single space, and then the variable last name. And then we'll print that new variable full name and we'll see we get this first string and the last string concatenated together with a space in between, making them a little bit more readable. We can use some of those built-in functions that we mentioned before that are called methods in order to create a copy of a string that might have some different format. So for our example on the left-hand side, we'll use the same string first name assigned to Harry. And we can print out first name and then we do a dot, so that's just a period. Upper, open parentheses, close parentheses. And we'll see that that prints out in all capital versions of the word Harry. Now notice it does not actually overwrite the string that is assigned to the variable first name. If we print that out again on the third line of code, you get the original string back. So it is just kind of a temporary copy of the string that is changed just for the moment. And unless you save that change, it'll only show up for that line of code and the string will revert back to first name when you see it again on that third line of code. On the right-hand side, there's a similar method that is called lower. So if you have a string that contains a mix of capitalization, like we have Potter there, all caps, we can display that using last name dot lower. And when we print that out, it'll all be lowercase. And again, it does not change that string as it's assigned. It only changes it for that temporary line of code. The next line of code last name is the same as it was previously. There are a few other similar methods that will be helpful for this course. The one on the left here is called dot title. It'll always uppercase letter, the first letter in a word. So if you had a string that contained multiple spaces, maybe a sentence, it'll always uppercase the first letter in each of the words on a string. And the one on the right, swapcase, we'll just swap the capitalization of every letter character that is contained in the string. There's a full listing of these on the course website and I'll link another set here in our lesson. So be familiar with them, these can be really helpful. We'll introduce more string methods throughout the rest of the course as they become more appropriate to what we're doing. Another vital piece of using strings in this course is how did you get just a part of a string? At its core, a string is just a collection of characters that are grouped together. And sometimes we'll want to use the entire string as a whole, but other times you might only want certain elements of that string. We can select certain characters from a string using what's called an index if we want just one character, or a slice if we want a selection of characters. Here on the screen now, we'll see we're indexing into the string that is named spell, and we're asking for the character that is at index zero. So that gives us that first E. Notice when we count in Python, we start counting with zero. So the word expel the armists, the letter E is at index zero, the letter X is at index one, the letter P is at index two, and so on. So if I were to ask for a spell indexed at the value of six, it's gonna give me that letter I. E is one, X is two, P is three, E, L, L, I is at index value of six. Now you can tell just by me counting it out, sometimes selecting characters that are towards the end of a string might take a while for you to figure out what the index exactly is. So we can also use negative indexes to work our way from the back. That S at the end of expel the armists would be at index negative one, and the U at the end of expel the armists would be at negative two. If you specify an index that is too big, so here we did spell index to 24, there are not 24 indices in the string spell, you're gonna get what's called an index error, and it'll give you a little bit of information there, it'll say string index out of range, meaning you have tried to go to a position in that string that does not exist. If we wanted multiple characters, say in a row, like we wanted the characters that are at index five, six, seven, and eight, we could do that by just cat-catenating those individual characters together, but it's a little bit lengthy, and in fact there's a better way to do this in Python using what's called a slice. On our second snippet of code here at the bottom, you can see we do spell, and we have the square brackets just like before when we were selecting a single index, but we specify the starting index and then a colon, and then the ending index, but actually one bigger. Notice I put index nine there, but it gives me back the characters that are index five, six, seven, and eight. So that second index you provide is what we call a non-inclusive index. So up to but not include the second value there. So you got to kind of just go one larger than you might think intuitively, but that allows us to select a subset of the string pretty quickly. If you wanted to say start at index five and just go to the end of the string, you don't need to count how many values there are in the string, you can just do five colon and then don't specify the ending, and it will allow you to select from index five all the way to the end. So that's a nice little trick. Likewise, you could say, count every other character. So I have spell square bracket, one colon, colon two. Notice just two things interesting there is that we skipped over the ending value. So it's going to go from index one all the way to the end. And that third value there of two says every other. So it'll go index one, index three, index five, index seven, and so on until it runs out. So we get back X E L A M S. You could have specified that middle value if you wanted. So this will go from index zero up to but not including index seven counting by threes. So we'll get index zero three and six or E E I. And you can specify just the last letter if you want to sorry just the last index if you want to so you could say colon colon negative one means start at the beginning and at the end and count using an index counter of negative one, you mean start at the end S and then go work your way back to backwards essentially to the front of that string. So S U M R A I L and so on. So a lot of different ways you can play around with string slicing to ensure that you get just the right characters that you wanted out of the original string. Couple other special things about strings or certain characters that have some special functionality. The first one here is that if you want to include a specific punctuation like a single character say you have the last name of O Riley which has the apostrophe well then you can't start and end your string with a single quote because when it gets to that apostrophe after the oh python will think you've just ended your string but in fact you had many more letters to go. So if you know that your string is going to have that special punctuation of an apostrophe or a single quote, you should use double quotes to start and end your assignment statement for a string that way python when it starts the string with a single quote it will only end it when it gets to the next double quote. Likewise if you think that your string will have some double quotes inside of it, maybe it's a passage from a book that's reading out a quote, you should start and end your string with a single quote. So you have to be a little bit on the ball to figure out what might you expect to show up on your string so you can use the correct single quotes or double quotes to define it. On the right hand side we have what's called an escape character. In my message I have h i and then a slash n and then world what the slash end does is when the python sees that when it's printed out so only when you hit the use that print function it sees the slash and it creates a new line. That's what the end stands for new line. There's a bunch of these others escape characters and I'll link them in today's lesson in case you're learning more about them. They won't show up too much in this course but it's good to know that they exist so that when you use those slashes sometimes you might get some unpredicted behavior if you're not familiar with these escape characters. So let's see how we can use strings to implement our rail fence cipher. Here we have a slide that we saw earlier in the previous part of our lesson that shows our zigzagging mathematics in the word grouped into the top and the bottom row. Let's think about how we might be able to use our string slicing to recreate the top row and then again to recreate the second row if we were provided a string mathematics that we've assigned to the variable named plain text. Think about which starting ending and step size you'd want to provide as the indices between those colons to recreate for our first group MTE AIS and for our second group AHMTC. Pause the video for a second and take a guess and when you're ready we can see the answer in 3, 2, 1. For our first group we could create that grouping of characters by specifying 0 and then a blank and then a 2 meaning start of the string mathematics at index 0 which is M and then count by 2 to also select the characters that are at index 2 T, 4, E, A, 6 and so on 0, 2, 4, 6 and so on. The second group will do the exact same thing except we'll just start at index 1 so we'll start at the character A counting by 2 so the characters at index 1 3, 5, 7 and so on which give us the AHMTC that we're hoping for. Now all we have to do is kind of glue those two pieces together to create our cipher text. So think about this how could you put it all together? How could you take a string mathematics and have a command when it's all done prints out the final cipher text? Don't worry about providing spaces in between the blocks of group of length 5 yet we'll write a function that will take care of that for us later on down the road for now let's just go ahead and make sure we get all the correct letters in the correct order. See if you can sketch out some code on your own and we'll go over the answer here in 3 2, 1 so here's one possible solution we assign the string mathematics to the variable named plaintext we assign the first group that we calculated on the previous slide to rail 1 the second group to rail 2 we can catenate those two rails together and assign that to the variable named cipher text and then we'll print the cipher text also using the dot upper method to ensure that it's capitalized remember we like our cipher text to be capitalized there are many other ways that you could have done this perhaps some with fewer lines of code perhaps some with more but the key is that we get the correct output for the correct input in this course we're not going to focus too much on the efficiency of your code meaning how many lines it took or how long it took for the computer to run it we're going to focus on the mathematical accuracy that ensures that the right plaintext goes to the correct cipher text that's it for this lesson on strings and the activity will push you to think a little bit further about how you can decode messages using the rail fence cipher and perhaps encode messages using rails of length 3, 4, 5 and more