 This video isn't very highly scripted, and I'm going to be doing most of it in the shell, so you have been warned. We've been using strings a lot in this course. A string is zero or more characters inside of quote marks. You can use single quotes like this, or double quotes like this, or triple quotes if you need a multi-line string. And you'll notice the backslash in, which is Python's way of saying, give me a new line. A string can have one single character in it, and there's a special string called the empty string that has no characters inside of it at all. Even though it doesn't have any characters between the beginning and ending quote marks, it is still a perfectly valid string. We've seen that you can add strings. For example, door plus bell adds them together. The official name is concatenates them, and we get a single string doorbell. You can also take a string and multiply it by an integer, and it repeats that word as many times as you need. There are no other arithmetic operators that work properly with strings. Strings are objects. With numbers, we would usually give the number as a parameter to a function. For example, the absolute value of negative 3.7. With strings, however, we need to use the dot notation as we did with turtles. Let's create a couple of variables here. Let's make the word playground, and let's make a sentence. This is a sentence. And let's have a variable called shout, which is all an uppercase. If I want to convert the word to all uppercase, I can't say upper of word. I can't use it like a number. Instead, I have to use the dot notation and say word dot upper. This sends the upper message to the word, if you're reading from right to left. And the result is playground in all capitals. In a similar way, I can say shout dot lower, which will convert all the letters in the string to lowercase. The exclamation point isn't a letter, so it remains unaffected. There's also the capitalize function. If I say sentence dot capitalize, it converts the very first letter in the string to capital letter. Notice that it capitalizes only the first letter does not affect any of the rest of the word, so it won't capitalize every word. It would be nice if you could do that, and later on in the course, I may show you a way to make that happen. There is one exception to this dot notation. If I want to find the number of letters or number of characters, excuse me, that are in a string, I can't say word dot length. That doesn't work. I have to say length of word. So that's the one exception to dot notation. I've shown you upper, lower, and capitalize. Another useful function is strip, which gets rid of leading and trailing white space on a string. White space is defined as blanks, or tabs, or new lines. Let's take a look at this program. It asks what's your name and tells you that's a nice name. This sep equals empty string at the end tells Python not to put extra blanks between the items that it prints. Let's run this and run it legit. I'll say my name is David. He says, that's a nice name, David. You'll notice that the cursor for the input was right next to the question mark. What happens if somebody decides that they're going to do this? What's your name? They'll put an extra blank and they'll say Joe. And let's put a couple more blanks at the end. The problem is that your name has those blanks inside of it. And so they'll say that's a nice name comma blank blank Joe blank blank exclamation point. Hardly a nice thing to look at. Here's what we're going to do. We're going to say that the new name is equal to your name dot strip. And then instead of printing your name, we're going to print new name. Now when we run it, if they say their name is Joe, the extra blanks at the beginning and end are gone. We can use capitalize. We could say this, we could say new name equals new name dot capitalize, which will now replace the old version with the capitalized version. And if they say Joe, everything looks really nice. Instead of doing it in two steps, I can make a chain of function calls. I can say take your name, get rid of the leading and trailing blanks, and then capitalize that. So I can do it all in one line as a chain of function calls. Now if the person says their name is Nancy, everything works out exactly as it is. And it's a very convenient way to do a series of function calls all in one go. There are a couple of other functions that you might find useful. If I say word dot L strip, it gets rid of blanks or white space at the left. If I say word dot R strip, it gets rid of trailing white space at the end of the word. You can also use strip in another way. I don't see this a lot, but it's there if you need it. Let's say I say that the word is equal to dash dash dash something and three dash four. Let's make a bunch of dashes there. I can now say word dot strip and tell it what the character is that I would like to get rid of. So I want to get rid of dashes at the beginning and end, and it does that. Again, I haven't seen a lot of use for that, but if you ever need it, there it is. One other useful thing to do with strings is to find out if one string is contained within another. Let's go back to the word playground, and I want to find if the letter A is anywhere in playground. I can say word dot find and give it the letter A, and it'll say yes, it's at position two. What does position two mean? It turns out that you can treat a string as though it were a list of characters. So when I have the word playground, the P is at location zero, the L is at location one, the A is at location two, and so on. In fact, I can extract any character I want to by saying this. If I want the word character at position three, that gives me the letter Y, word sub zero, and later on by the way you'll see why I'm calling it word sub zero, why I read it that way, is the letter P. This is called indexing. The indexing operator is a square bracket, and the number in between the square brackets is the index that you're looking for. What would happen if I tried word of negative one? The answer is it would give me the last letter in the string. Word negative two would give me the N. What happens if I go off the edge of the universe? The length of the word is 10. So if I try to get to word 11, it'll say the string index is out of range. That index number is too big for the word. Same thing if I go off at the negative end. If I say word sub negative 11, it'll say that the word index is out of range. Let's go back and look at word again. I can do find for more than one character. If I want to say word dot find YGR, does YGR exist anywhere in playground? The answer is yes, starting at position number three. What if I try to find something that isn't in the word, like Igor? Answer is it gives me back a negative one. That doesn't mean it's the last character. The negative one is special. It means not found. Let's take another word here. Like bookkeeper. I like that because it has three sets of doubled letters in a row. And I want to find the letter O. It will find the first occurrence. It won't find all of them. It will find the first O, which is position number one. Similarly, other dot find of K. We'll find it at location three. Zero, one, two, three. So find gives you the index of the first occurrence of a string inside of another one, or negative one if it's not there at all. There's a function called index, which does something similar. If I say word dot index of GRO, it'll give me four. And that's correct. Because if we look at it, zero, one, two, three, four is where we have GRO starts. However, if I try and find something that's not in the word like GROW, it'll give me an error. If you want your program to give an error when something isn't found, use index. Most of the time, though, you'll want to get back a negative one rather than getting an error, so your program can continue and do something useful, usually using an if statement.