 Welcome back everyone this is Brian we're gonna continue our journey on Python 3 and we're gonna continue with basic string operations now When I said strings are complex data types. I was not joking and there's no way we can cover everything in these little two videos Especially because we haven't even covered the fundamental logic of programming So we have to stick with the basics for now. Don't worry later on in this series We are going to revisit some of the more advanced things we can do but right now you have to learn to crawl before you can walk So let's start crawling Gonna say variable str is going to be Hello world. This is a string Very very simple and we're gonna start off with here. So What we're gonna do in this video is we're going to do things like getting the length of the string Repeating the string replacing characters and things of that nature and even slicing them and getting the specific indexes or Positions within the string. Let's dive into the basic operations. So first things first Let's say we want to get the length. I'm gonna say print We're gonna call the lin function, which is not limited to just strings, but it is super super handy for strings And we are going to get fit just print out a comment here Get the length We run this we can see this is 30 characters long Now you might be inclined to say no wait a minute. It's a zero based indirect So it's actually 29. No, it actually gets the length not the position. That's fundamentally different as you're gonna see later on So now that we've got the length we can do other things as well Well, let's say we want to repeat a string and this is gonna hurt your brain just a little bit here We've talked about how you cannot do mathematical operations with a string Remember we were trying to add an integer and a string together Well, you can do what's called string math and this is what I mean by it's gonna hurt your brain We're gonna say str times three Now, I know what you're thinking You're thinking you're gonna get some sort of weird thing because you're trying to multiply But actually what you're telling Python to do is take the string and You guessed it multiply it by three and return a giant string. Let's demonstrate Hello world. Hello world. Hello world. So it did exactly what we thought it would do here Yes, the first time I did that I kind of sat back in my chair and went wait, what is that right? But it is actually a thing with Python if you're coming from another language You're probably sitting there just staring at your screen going what witchcraft is this but it's actually super handy if you need to repeat a string Now let's go ahead and let's look at replacing and If you're coming from another language, well, this is exactly what you think it is. It's just dead simple So in Python strings are a data type, but they're also a first-class object meaning They have functions built right into them. We haven't really covered functions yet But just know you can say you're a variable name dot and then call some code and we're gonna call the replace function and what this is gonna do It's going to take the string and replace a section of it. So let's say I want to replace Hello with Hola So if you're from Mexico or Spain or any Spanish-speaking country, that would be the correct way of saying hello as hola I'm probably mispronouncing that but you get the point. You can simply replace it. So Hola world. This is a string Makes it super simple to do that. You don't have to figure out where things are You can also do something like split a string So if you're coming again from another language, you've done this before And I'm gonna say SDR. I want to split and notice how it's looking for a separator here So let's go ahead and split this on that comma If you're not coming from another language, you're like, wait, wait, wait slow down. What is splitting? So we're gonna take this string and Turn it into two strings and we're looking for a separator value in this case this comma right here So it's going to say hello world and then this is a string and it's gonna give us two strings back Here we go Hello world and this is a string now. You may be going now. Wait a minute this commas here Look at these little brackets. You got this in brackets beginning bracket. What it's done is it's created a data type We haven't talked about yet, but we will in the next few videos and it's put two strings into that data type And then handed it to us very convenient way of saying hey split those up as you go on programming You're gonna actually use that quite a bit Now let's say we want to know if this starts with something so I say SDR and I want to say starts with Does it start with the letter H? I'm almost embarrassed to type that comment starts with because it's pretty self-explanatory what it's doing But just in case it's going to return a bull and it's gonna tell us hey Yes, it does if we switch this to like J. Does it start with the J? alts Very simple handy way of determining what's going on We can through the magic copy and paste switch this to ends with And let's say we want to make sure this ends with an exclamation true true there we go and Let's go ahead and look at uppercase lowercase and capitalization. We're just going to say print And we want upper It's gonna give us hello world This is string all in uppercase you notice how it's got these brackets here And that's because it is a function if we would omit those We're gonna get a built-in upper of string object and then this number you ever see something like this Basically, what you're trying to do is call a function without It's brackets and you need those you're wondering what this number is. That's a location in memory So an object is simply something that exists in memory and that's its location So admittedly this message is not super helpful for beginners, but I just wanted to explain what that was Around this out. We'll say Lower and I want to capitalize So now we have all uppercase all lowercase and capitalize the way it should be Let's take a look at slicing and when I first heard this term I actually I'd like this vision of whipping out a lightsaber and slicing something in half and it's actually Not far off from what we're talking about. We're talking about getting a substring now when I say a substring remember This string is just a series of characters and each one is at a position So the zero would be here and then one and so on and so on and so on and so on So what we want to do is get a substring or a slice Think of it like you have a pie in front of you and you're gonna get a slice of pie You're not taking the whole thing just apart. So for example, I could say I want the word world or I want just TH out of the word this or this specific space Or I wanted to get everything in the end up to that point You can do things like that very very rapidly in Python and this if you're coming from another language I'm gonna tell you is extremely cool once you wrap your head around it. So we're gonna say print And we're going to take our variable now, we're going to put those brackets there that indicates we're getting a slice The format here is very simple. We want the start It colon and an end position So the start position in this case, we're gonna say the zero or the starting position And we're gonna end in five and what this is going to do is it's going to get the first five This is a zero based index Let's print this out and see what happens here One two three four five. Hello is five letters. There we go. So it did exactly what we're trying to do here Like I said it looks a little confusing at first, but once you start wrapping your head around it It's not super hard Now I want to start at the sixth position and I'm going to omit the ending and What we're doing here is we're saying We want to get from the sixth position all the way to the end So when you omit something it automatically defaults to the beginning or the end depending on which one you omit So the sixth position would be you guessed it World all the way over here Ta-da works as expected Let's go ahead and grab this and Let's try something a little bit different We're going to start at negative seven now you may be going wait what negative how can we have a negative seven? Well, when you have a negative you actually start from the end So because we're starting with a negative it's going to start back here and count backwards Actually pretty cool how that works. So let's run this and The last seven is string exclamation pretty pretty cool Try doing that with some other languages in some are going to be very cool Some are just going to completely Infuriate you depending on the language and now let's get a substring. We're gonna say from 6 to 11 and we want to get 6 to 11 just for our notes here See what that looks like and it is the world world Very cool very simple very easy Now if you're coming from another language, you're probably still stuck on this right here Don't worry whenever you see that negative symbol. Just think you're starting from the end and working backwards Now slicing is cool and all but it's not super helpful unless you can actually Automate the way of getting the number because no one wants to sit here and count things, right? So let's look at how to get the index or the position of something What I'm gonna do is I'm going to Say L equals And we're gonna look for the comma now if we look at our original string We've got this comma right here, but we don't know the position. It's at so I'm gonna hide that off the screen And we just want to know hey we want to look for this We're gonna do it two different ways. We're gonna say C equals SDR find This is what I love about Python. It's very very Intuitive fine does exactly what you think it would it find something It's gonna tell us what we're looking for is the L and it's gonna tell us where it's at Or if it doesn't find it I'm gonna say negative one if not found Let's go ahead and say print and I want to say find return C Let's run this see what it does So whoops actually misspelled find there easy fix So find return C. So it is at the 11th position. We didn't have to sit here and go One two three four and we'd be here all day doing that instead. We want the computer to do the work for us So we know this at the 11th position now if we change this To just something Let's just do a single t You see find return negative one So in this case when you see negative one you can say it's just simply not there It's not going to return a zero because remember zero is the starting position So Find is really really cool. But if you're coming from another language, you're probably looking for index of which is something totally different So I'm gonna say i equal sdr index We're gonna give it the same thing the l And now we want to this And we're gonna say find return i And let's see what this does now remember we have this t in here Where is t in here? It's well right there, but it's a lowercase not an uppercase So it should return a negative one or will it actually no it does not Instead it gives you what's called a value error substring not found This is a convenient way of saying hey that must exist or throw an error something. We're gonna cover in future videos Just know that find will not return an error and index will return an error So most of the time you're gonna want find but if you're coming from another language you think you want index and you really want find Oh super confusing sometimes We'll throw an error Just want to make sure we put that in there just in case And let's switch this back And you see they both return 11 Because we're looking for that comma remember index will throw an error find. We'll just simply return a negative one Wrapping this up. Let's go ahead and say we want to create a new string from the substring. How do we do that? So we want to say x equals sdr and we're going to slice that string We're going to emit the starting position because we want to go from the beginning And we only want to go to the position of this comma If that seems super confusing, let's slow way down. So we have a string And it says hello world comma. This is a string exclamation So we're looking for this guy right here, which we found at the 11th position And we're saying okay, so from the very start Hello world actually just going to copy this whole thing right down here Copy this put it right here as a comment There we go So we're going to say so from the very beginning right here All the way up to the position we find We want to create a string and call it x now. We want to take that and just simply print it out Hello world super super simple So quick recap Strings our first class objects in python. They are unicode by default there utf-8 Although you can specify some other way of doing it google's your friend if you need to do that immediately And you can do some really cool things like get the length Repeat it replace it split it make sure it starts with ends with upper lower Capitalization you can slice it dice it do whatever you want to do and you can search for or find things Within the string And if you need to throw an error if it doesn't exist you can use index which I do not recommend because It's not really a good idea to throw an error in your code most of the time and You can create your own strings from substrings very simply very easily 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