Equally important to numbers are strings. Strings allow us to work with text and to process words and characters in our applications. Let's take a look at some of the basics of using strings now.
1.Introduction2 lessons, 11:32
2.Python Building Blocks6 lessons, 1:08:07
3.Controlling the Flow7 lessons, 1:20:10
4.Common Data Structures4 lessons, 46:49
5.Application Structure7 lessons, 1:15:12
6.Collections7 lessons, 46:55
7.File I/O6 lessons, 48:51
8.Networking5 lessons, 43:48
9.Connecting to Network Services3 lessons, 34:27
10.Conclusion1 lesson, 02:08
The next foundational building block that we're going to work on within Python is the string. Now strings can seem very unassuming to the untrained eye, but there's really a lot going on there. So let's start off with the absolute basics, how do you create a string and exactly what is it? Well, there's a couple different ways that you can actually create a string so it's not just saying let's create a string, there's actually multiple ways. So let's start off with a very simple concept, a very simple string. And we're gonna say this is going to be hello. So if I wanted to create a string called hello, I could use either double quotes or single quotes. So I can say hello with double quotes. And as you see here, it's actually going to change it to single quotes. And then I could also say 'Hello' and it's going to give me the same string. So, when it returns back to you, you're gonna see that it's gonna give you the single quotes but you can define it either way within Python. So let's get a little bit more complicated. Let's say that I wanted to create a string with a word that has an apostrophe in it, which is going to look like a single quote. Something like don't. So if I wanted to do single quotes, I could say don't, don apostrophe t. And then put another apostrophe there, that's gonna cause a problem. Because, how do we know that I really want this apostrophe here to actually be included in the string? How do we do that? Well, there's a couple different ways. If I want to define this string using single quotes I can do what's known as escape. That quote that apostrophe within their using a backslash. So if I were to do that you're going to see now that it is change this to be double quotes on the outside. And then a single quote in the middle so that is also another way that I can combine these two different types of strings or creating two different types of strings into one. I could also use the double quotes and I could say DON apostrophe that's a single quote T and then a double quote at the end and that's going to work as well so basically if you're using very simple words without that sort of apostrophe concept in there then Python is going to say, okay, great. Here is hello with the single quotes, but if I'm going to include that in there. If I want that apostrophe to show up, I'm going to either need to escape it, or I'm going to need to use the double quotes. Another more interesting kind of strange way I could do it is I can flip flop this is well. I could say, single quote DON double quote T and then single quote and that's going to give me kind of the opposite that's not going to really make sense per se but I can do that the other way as well so if I wanted to deal with something like a quote I wanted to within the string, leave a quotation. I could say something like using the single quote method I could then embed a double quote, and I could say something like Hello there, double quote. Or actually, comma double quote, I said, and then end with a single quote. So on the book ends I have these single quotes and then I can actually put a double quote quotation in the middle of my string, and then end it with just a plain I said. And then I'm gonna get back there that full sentence that contains both the single and the double quotes. So just something to keep in mind, that I can combine the single and double quotes. But just remember, if you start with a single, you have to end with a single. If you start with a double, you have to end with a double. Because if you start to mess the two up and if I were to start with a double and then say hello and end with the a single, then that's gonna become a problem. So always start and end with the same things and if you're going to use something like single quotes and then have an apostrophe. You're gonna either have to escape it. Or you're going to have to start and end with the double quotes, and use the apostrophe in the middle. Now that's all fine and good, but not very often are you just gonna start typing in strings like that within your Python applications or within your Python programs. You're gonna do things with them. And one of fairly common thing to do with them is to actually print them out to the screen. So this is gonna be an introduction to one of the first built in functions that we're going to see throughout this course, and that's going to be the print function. Now, the print is pretty versatile, but basically what it's going to allow us to do is is it's going to print whatever it is we want out to the screen. So let's say we took one of our previous strings here, the Hello there I said, and I wanted to save that as a quote. So I could say quote is going to be equal to. So now I'm going to save this string into the quote variable, so that at any point in time I could just bring it out, and I could say give me the quote. Oh, it's Hello there I said, but I could also use the print function to then print out quote, and it's going to do the same thing. So typically, what you're going to do within your applications is, you're going to use print. So what's the big deal, you say? Well, print is going to do some behind the scenes trickery for us that just typing in string literals, which is basically what we're talking about right here, just typing those in, print is going to do some other things for us. Let's say I started to work on a string that I wanted to embed a newline, or something that was going to put part of the string on the first line and then the rest of the string on the next line, and that's where I would use something called the new item. And within Python, that's gonna to be backslash N. So that's going to say I want to split these things up a bit. So let's say I kept my quote here, and I wanted to put in a new line in just kind of a random spot, let's say I wanted to put it right here in the middle. I wanted to have it say the quote hello there, comma, and then I wanted I said on the next line. Well, if I just do this and I say all right give me quote. Well, it's going to think that the slash N is just part of the string. But at this point, if I were to use the print function and I were to print quote, you're gonna see that it's going to honor that slash. So print is going to allow us to use some of these special characters like the new line character, and actually interpret it as it's supposed to be done. But there's definitely times when that can kind of pose an issue for you. So let's say I was dealing with a file path, and the path, in my case since I'm working on Windows, is going to be something like see C:/. And let's just say that there's the name of a folder, something like that. So now I have path and I can see where that's at, but if I want to say print(path), well it's gonna print out this kind of goofy looking thing here. I'm gonna get C: on one line and only aim on the next line because it's interpreting this /n as a new line. But what if I didn't want that to happen, what if that was not the case? Well then I could do something within Python, known as specifying it as a raw string. So let's say I could do print. And just like I did before, let's say I just wanted to do see C:\name, just like we saw there but now I can use the R in front of this string to say I want this to be a raw string so then it will then print everything out here and it will not interpret the slash and or any other sort of special characters as special characters, it will just treat the entire thing as a raw string. There's also some rather interesting things that you can do with strings in Python so you might think okay. Well, yeah I can just do string types of things. I can declare them and do some other things with them, but what about mathematical operations? Well I can concatenate strings. And what does concatenate mean? Well that means I can put the two things together. So let's say I had first is going to be equal to Derek. That's my first name. And then last is going to be equal to Jensen, which is my last name, and let's say I wanted to put those two things together. Well I can do first plus the last. Well, that's gonna look a little strange cuz there's no space in the middle, so let's go and fix that. So now I can do first plus, and I can do an empty string plus last. And now I'm gonna get a nice little space in there so I can add these strings together, which basically means concatenate or sew these two things together or sew these multiple strings together. I can also do multiplication. So I could say first*3. Well that's gonna print first out three times. So then I can put a bunch of these strings together by a certain number that I'm given to it. And I can even combine goofy things together. And I could say something like, first plus. Then put a space in here, plus last. And then I can multiply that entire thing by three. So now you're gonna see my name put together three different times. Another concept you may be familiar with in other languages, is the fact that a string is actually an array of characters. And that basic concept still lives within Python as well. And we can start to pick apart a string by asking for a character at a specific location within that string. So, let's kind of go back here a moment and come back to first again. So, if you recall first is equal to Derek. Well, what if I wanted to pick this apart a little bit and I wanted to just get the R out of my name. So that's the one, two, three, the third character within that string. So if I wanted to pull that out, I could do something like first and then do open close square brackets, and then give the index of the value that I wanted to get out of there. Now I said that was the third value in there. So, if I put in three and hit return, it's actually going to give an E, and that's because within Python, as with many other programming languages, when we start to talk about indexes, they're zero based. Which means yes the R in here is the third character. But because we're zero based, the D is 0, the E 1, and the R is two. So if I wanted to get the R instead of saying three I would actually have to say to two so now I could start to pick apart this string and just get out the third character by using the index of two. So now not only can I give it positive values to go ahead and get a specific index or a specific value at an index out I can also give it negative values. So what exactly does that do? Well if I said I wanted to give it a -1, I'm actually going to get K. So if I specify negative numbers within this index it's actually going to go one space but it's going to start from the end and work its way backwards. So just like -1 is going to give me K -2 would give me the E, and -3 would give me the R. So now I can work forwards and backwards using indexes on these strings. Now strings also support something in Python known as slicing and you can also think of this as getting a substring, really, if you want to think of it that way. So if I wanted to get a substring or slice first. So remember first is Derek. If I wanted to get just the first three letters, I could slice this by saying first. And then once again, we're gonna open and close square brackets and then we're going to specify the index that I want to start at, so in this case I want to start at zero. And then I give a colon. And then I specify how many of these values that I want or the index to which I want to go to. But the tricky thing here is that the first index is going to be included in the slice or the substring, and the second number is going to be excluded. So I want to go from zero to two. If I do that, I'm just going to get DE. So, like I said, the first index is going to be included so that's D, and the second one is executed. So if I wanted the first three, I'd actually have to say zero, two, three. So that would give me Der. Then you can kind of play around with the slicing a little bit, and I can say something like, well I want everything from the first value which is actually the second which would be this e, all the way to the end so I could just specify the colon and not give an ending. And it will give me everything to the end of that string. And then I can also do somewhat the opposite and I can not specify the first, which is going to assume that we want to start at the beginning. And I want to go to three which will once again give me DER. So that concept is referred to within Python as slicing but you may have heard of it in other languages and other technologies as getting a substring of a particular string. Now once again just like we talked about little problems that can creep in when we were working with numbers, the same thing can happen when we're working with strings. So once again remember with first we're dealing with Derek but what if I were to say something like well I want to go into first and I want to get the tenth character. Well, there's not that many there that's gonna give me an out of range error which is quite common to find within programs as you're beginning to get started. So, be aware of that because you can definitely run into a problem where you could ultimately be working with a limited number of characters or limited number of values somewhere, and you could ask for one outside of the range and ultimately run into problems. And one way to make sure that you don't run into those problems is by kind of keeping track of the length of a particular string, which is a very common operation as well. And in order to do that we're going to use the len function which is going to take in was a single parameter in this case. And we're gonna say we want to get the len first, the length at first, and that's going to say it has a length of five. So as you can see here now we're given enough tools to be able to do some slicing and dicing, some combining and working with basic strings, and using the print function. And how we can actually start to use these string operations to combine strings and slice them apart. And now we want to talk about the final building block within Python, that we're going to talk on quite a bit about and then use quite a bit throughout the rest of this course, and that's going to be lists.