 Welcome back, everyone. My name is Brian and we're going to talk about strings. So what is a string? That's a very good question. We have before you a simple diagram. The word hello H-E-L-L-O. And there's some numbers here and some numbers here. We're going to explain all this. So really a string is a list of, you guessed it, characters. And each character translates to a numerical value. So if you look down at your keyboard and look at like the letter H, that letter, depending on whether the shift key is pressed, will be 104. And when I say depending because there's a numerical difference from lower case and uppercase, we're talking about lower case here. Some people will say, oh, that's ASCII. I've heard about this before. This is actually not ASCII. It's Unicode. Specifically, UTF-8, I think is universal text format, 8-bit or version 8, I forget, but basically Python under the hood seizes as UTF-8. So it doesn't matter if you're typing in English or Korean or Spanish or ancient Greek or some mathematical algorithm, there is a number assigned to each letter. Computer knows what that is and takes care of all of this for you. So you don't have to worry about it. The two major things you should focus on is, well, the letters themselves that form the string and this down here, which is the position. Everything is in a list. So when you look at it, H-E-L-L-O, there are five letters there and there are five numbers starting with zero. This is a zero based index, zero, one, two, three, four. So if I say give me the letter at the third position, H-E-L, this guy, that is actually number two because it's zero based. It's a little confusing if you're new, but you're going to go, no, wait a minute, that's actually number three. Remember, it starts with zero. So the first one's always zero. We're thinking in terms of computers, zero, one, two is this guy right here. Once you wrap your head around that, you know more than most people walking around. And honestly, it's not super hard. You just have to understand that everything you see on the screen has some sort of number attached to it. And the computer handles that number. You just need to worry about what letter and what position and we're going to go into that. Okay, let's flip over into Visual Studio Code here and we're just going to copy and paste some code here. And this is going to look like absolutely nothing you've seen before. Don't worry about it. Just taking a leap of faith. We're going to cover this in a future video. But really what we're doing is we're saying 4X each letter in the string hello. We're going to print out the letter and its numerical value. And you can see 104, 101, 108, 108, 111. Burn those into memory. 104, 101, 108, 108, 111. If we flip back, it's exactly what I told you it was going to be. So let's dive in here and figure out what's going on. Now, again, we are going to cover loops in a future video. That's not this video. We are hyper focused on strings. So the first thing we need to do is baby steps how to make a string. Very, very simple. Simply make a variable. And assign it. Let's do that again. Notice how the first name or the first variable has double quotes where the last name or the second variable has single quotes. In Python, you can do it either way. And they do this not so much to confuse you but simply because there are a bunch of little gotchas that you're going to find later on in your life as a programmer. And this is super convenient that you can switch it around however you want. So the first thing you're going to want to try is just simply to, well, merge them into one larger string or print them out. So we're going to print, I want to say first, plus, and last. Now, what do you think is going to happen here? You notice how we're using the plus sign. If you're a math nerd, you're going to say, oh, this is going to kick out some weird number. Actually, no, it's going to say, you guessed it, it's going to, I think the term is called concatenate them. It's going to merge them together into one string in memory. So it's saying the first Brian with a space and last Karen's very, very simple. You can also do something called formatting, which we've done before and it really does help you avoid errors in the long term. So you can say something like this print F and notice how I've got quotes doesn't matter if you're doing single or double. And I'm going to say, hello, my name is, and then let's see here. There we go. I had to look at my keyboard to figure out where that was. And then we just type the variable name first and then last. Now we tend to use formatting to avoid issues. You've seen me do it already, but you probably haven't really realized why we're going to cover that a little bit here. So hello, my name is Brian Karen's to kind of compound that in your mind. Let's make a variable called hers and we're going to say others. Notice how I'm mixing and matching these. I'm using double quotes. So Python knows, hey, this is the string, but we could also use single quotes. So that gets super confusing. What it's going to do is take the first one at C's and says, oh, you're using double quotes to make the string. So if I were to change this to a single quote, notice how this letter suddenly turned white. Even if I end it in a single quote, it's going to get really, really annoyed with me. See, boop, syntax error and valid syntax. If you ever see a syntax error, really what Python's telling you is you screwed something up and it'll tell you exactly where in this file on line 11. And hers and then there's a little arrow right here under S. It doesn't know what to do with this because it is not in the string. Change that and put the double quote there and it magically fixes it. See, doesn't make a whole lot of sense. So let's change that back. This is why you can mix and match these. It makes it super, super simple. And we can just print that out and it works as expected. There's our little single quote right there. And we don't have to worry about any special encoding or formatting or anything like that. Okay, so to solidify this under the hood, a string is a Unicode series of characters specifically formatted in UTF-8. If you want something other than UTF-8, you're going to have to go out and Google how to do that. I'm just saying we haven't covered it yet. We may cover in the future, but if you need something immediately, definitely go out to Google. A string is a sequence of one or more characters and those characters are numerical values. And that's what we're really going to drive home right now. So let's make a variable. Let's call this S1. And we want this to be a character. Notice how it's looking for an integer. Return a Unicode string of one character with ordinal and then it gives you some examples there. And I'm going to say, because I have this baked into memory, 72. And then we're going to make another one. And this is character and we're going to say 105. Now, you don't see it, but I'm sitting here cheating a little bit. I'm looking at the UTF specifications and the character tables, which you can see definitely by visiting these links and other links. And it will give you the numeric values. Now this is if you wanted to do something like this, you really don't need to. I'm just demonstrating it can be done. So we're going to say S1 plus S2. And let's kick that out and it spells the word high. Notice it's a capital H lowercase I, 72. Now, lowercase H is 104. Uppercase H is 72. This is what I mean by there's a fundamental difference between upper and lower. Now you may be inclined to say, now, what about bold and italic? No, that's actually not part of it. It's not letters. Those are just simply styling and formatting of what you see on the screen. So really all we're talking about is upper and lower case. It's actually goes way, way beyond that. You can do something like this to say print. And I'm going to say character. And I really have to cheat and look at my notes for this one, 8710. And this goes way beyond ASCII. Let's go ahead and save and run and see what this looks like. Boom, it's this little mathematical symbol right there. That's right. It's got math symbols baked right into it. You can do some really cool things with just simple characters. You may have heard the term escape characters. Now, what is an escape character? It's not something out of a novel, although it could be. But basically instead of memorizing all these weird numbers, they have it built right into the language. And actually most languages and even operating systems, they're called escape characters. What it means is you can escape a string and print a special character. And let's take a look at how this works here. So I'm going to say print. And we're going to even format this just to show you how this works. I'm going to say hello and then world. And notice I've got that kind of jumbled together. Now you can do something like this. You can say, okay, I looked it up and it's, you know, 18 for a hard return. Plus I want to do like a character 10 for a line feed. And that will put this on two different lines. And I had to go out and look up these numbers because I didn't memorize them. But it does work. Hello world. Or you can just simply do something like this. And we're going to grab it. Get rid of all that nonsense. Slash r for return slash n for line feed save run. Does the exact same thing. So under the hood. This little guy here slash r. Notice an escape character starts with a backslash here. We're saying slash r for return or n for new line. Does the exact same thing. You'll see that out in the real world both ways where some programmers will demand you use the character and some programmers will demand you use the escape character. It gets a little confusing until you get used to it. But it's super simple. And you can do things like this. And really all we're going to do is we're going to print out hello world with a tab between them. Slash t for tab. When in doubt you can go up to Google and you can type out what is an escape character. And it will give you a complete list of them. There's tons and tons of them but they're very simple and easy to work with. Here's hello world with a tab in between. You may be wondering why even use escape characters. I mean it seems a little rudimentary. Now let's go back way back to this problem. We want to put that in there and it works fine if we do it in double quotes. But the minute we change it to single quotes remember we're going to have some sort of issue. It's not going to know to do with X and it's going to say you guessed it invalid syntax. Well instead of going through and re-changing all our strings we can just simply put slash in front of it. And most of the time that will work it'll escape it right out. Go ahead and save to clear that error out. Let's actually clear this out just to show you it will work. No more syntax error and we can print this bad boy out. So escaping actually becomes a very convenient way of well breaking out of the constraint of which quote you should use. I know I'm going to get that question constantly of should I use single or double and really it does not matter. Just pick one and roll with it and if you need to switch them around you can always escape out of them. It's not super hard. You can also do things like this and this is something I get a lot of questions about not just with Python but pretty much every language. So I'm going to say quote equal. You see what we're doing here. You can have multiple escapes. It doesn't really matter. Basically what we're saying here is once you do this slash the computer will try to figure out what you're doing and it will roll with it. If it can't figure it out it will give you an error message at which point you're going to have to probably do something like this or you're simply using the wrong escape character and when in doubt go Google it. It's usually pretty easy to work with here so we're going to go. Then he said quote unquote hello to me. Now you may be wondering what's the deal with formatting when you read books or watch videos on Python they would go really in depth in the formatting and why it's important really you format to avoid errors especially with strings and let me give you a very simple demonstration here I'm going to say name is Brian age is 46 and boy I feel 46 today I was raking leaves all day yesterday so wow yeah that sucks. Alright so we're going to print these out I'm going to say name plus and we want the age. Oh this will work and it will work beautifully right? Well guess what? No it does not. There's a reason for it. It's not very intelligent when you first look at it so we're going to say line 36 in this module print and it doesn't put the little little arrow but it does tell us must be str not int so what it's really talking about is this thing what you're trying to do is take all of this and treat it like a giant number and then add these together we're trying to do basic numerical operations which we already covered and Python's not going to let us do that so let's get that out and let's just put a note here and let's show the correct way of getting around this now there's a few different ways we can do the way we've been doing which is we just put an F in front of the string and then we're saying we're now formatting it not very hard we've done this before but if you want a little bit more control there are other ways of doing it we're not going to dive into every single possible way I'm just showing you the two that I use the most and this is the next one I'm just going to print and I want to say let's do double quotes why not my name is here what we're going to do is we're going to say we want percent s and what we're doing here is we're making our own special custom string with formatting baked right in I am and then I want a percent I for integer from here though now we need to do a percent sign and tell it hey we're going to feed you some values those values are going to be name and age now I typically don't like doing this because you have to read this whole thing skip over to this percent sign and then look at this little guy here and figure out what we're sending it okay so name is the first string okay and then age is the first integer hmm alright so let's run that see what that looks like and it says my name is Brian I'm 46 years old let's play around with this let's put I and S just to see if we can easily break this and sure enough we've broken it with very minimal effort type error so really I tend to favor just the simple formatting because we don't have to mess around with any of this or worry about screwing this up but if you need special formatting it is super super simple to put it right in there when in doubt Google is your friend and there are honestly thousands and thousands of tutorials on how to really learn that in depth we're not going to spend a lot of time on it though I hope you enjoyed this video you can find the source code out on github.com if you need additional help myself and thousands of other developers are hanging out in the void realms Facebook group this is a large group with lots of developers and we talk about everything technology related not just the technology that you just watched and if you want official training I do develop courses out on udemy.com this is official classroom style training if you go out there and the course you're looking for is just simply not there drop me a note I'm either working on it or I will actually develop it I will put a link down below for all three of those and as always help me help you smash that like and subscribe button the more popular these videos become the more I'll create and publish out on YouTube thank you for watching