Strings Recorded Transcript This transcript was created with a speech to text software. Please excuse any typos or mismatches with the video. Hi, welcome back in this video. We're continuing with our Python prep series and we're gonna work on strings. Uh strings are ways to contain text in Python. Uh And why don't we go ahead and take a look in our Jupiter notebook. So in this notebook, we're going to discuss what are known as strings in Python, often denoted as S T R. Uh Again, if you have experience with coding, it might be possible for you to skip this video and move on to uh different things in Python that we'll talk about later. However, I suggest that you just scroll through, see if it's something that you feel like you're comfortable with. Uh If not, please join us in working through this video. So a, an S T R, as I said is a string in Python uh which is just saying that it's a piece of text. Uh for example, just a single word, perhaps an entire sentence, a paragraph or maybe even a book. Uh So in the Python documentation, you can click here and see what they have to say about S pr s or as they're calling them text sequences. So a sequence is a particular data type in Python. We'll talk a little bit more about this in the next uh video I believe uh we are not going to call them sequences. It'll be called something else, but just know that you can click here to learn more about S T R S in Python. So as a simple uh example, why don't we make a string in Python? So we'll hit shift enter and we can see here that this is a string. So we have some text and then we define it to be a string by putting these double quotation marks that I've now highlighted these double quotation marks around the text are what tell Python that this is a string. So in Python, we can tell our computer that we're typing a string by putting the text within a pair of double apostrophes uh or double quote marks or a pair of single quote marks uh or apostrophes, whichever you'd like to call them. Now, the key here is if you're going to use the double quote marks, you are allowed to use single quote marks within it like I have here. However, if we were to try and put something like a double quote mark in here, we can see that we have now um destroyed this and if we tried to run it, it would cause a syntax there. So let's go back to what it was before. So uh just also know that vice versa could be true. So let's say I wanted to use the single quotes, then I would be allowed to use double quotes inside of the string, um but not single quotes without an extra modifier that we'll see a little bit below. So let me go back to the original version that I had typed. OK. So another way that you can write a string is between uh a set of three quotation marks, double or single. So here's three quotation marks in a row followed by three quotation marks in a row. Why this is nice as it allows you to break your line without having to create a separate string object. So using just a single pair of quotes, we would have to do something that we'll learn in a little bit called concatenation. Here, we can just break the next line. So here is a string that spans multiple lines and now you might be wondering, well, uh wait a minute there, Max. Uh This isn't exactly the same as what I typed. I'm now seeing this thing called slash N. So this backslash, I think it's a backslash. This slash N is what's known as a special character uh in Python. And it tells the Python uh when you go to print a string or write a string to file, which we'll do in a, a later notebook. Uh It tells it that you want a new line. So there are a series of characters that have a special meaning in Python, perhaps some of the most important uh not, you know, not the only ones, but some of the ones you might encounter most frequently or you have slash N, which makes a new line slash T is a tab. So it's similar to the tab key on your keyboard slash single apostrophe would be how we'd want to write an apostrophe in single quotes. So for instance, here is a single S T R. ... So if we were to put, as long as uh you notice here, our string is denoted by a pair of single apostrophes. And if we put slash apostrophes within, we're allowed to do this, OK? ... Uh And then similarly, if we wanted to do it with double apostrophes, you could do the following. ... OK? All right. Uh And then finally, if you might wanna want uh do a backslash, so this backslash as we've seen denotes a special ca you know, is telling Python, hey, a special character is coming or if you want to actually type one, you could do something like this, just backslash backslash. OK? All right. ... Uh Let's delete that. All right. Uh So now you might have noticed that like, well, what if I don't want this to show the slash? And so you can, this, uh if you want to display the string for somebody to read, uh you can print that string. So this is a print statement, a print statement takes in a string or a series of strings separated by a comma and then prints it. So here we'll see if we print that string we had before. Now it's on sort of a displaying in the way that we mend it when we typed it out. So here is a string that spans multiple lines. So prints are useful when you want to track the progress of some code you're writing uh in a little bit and a few notebooks. I think we'll see what's known as a loop and perhaps you're running through some loop and each time through you want to do something, but it's a really long loop. So it, it runs for a long time, but you wanna be able to check the progress, maybe you'll print out that progress uh in your notebook. OK? So now we're gonna do some exercises once again, I'm encouraging you uh to pause your video, try to do the two exercises on your own and then come back and see if it matches what I did or feel free to try and work along while I uh explain it. OK? So what do you think will happen when you run the following code? Think about it before we execute the block. Now, if we remember in an earlier video, I mentioned that when you're running a code chunk in a Jupiter notebook, the thing that gets spit out at the bottom is the last uh uh Last returned object in your code chunk. So if we were to run this line one happens before line two. And so therefore, we should see line two returned as an output and line one not show at all because line two is the last possible output in this code chunk. OK? And so that's what we see. Now, let's take this and copy it for the next question. Copy and paste the code junk from above done. Uh So what if I put these both inside of prints? ... So I'm putting them inside of print now. And when I put them both inside of print, we can see that we now see both line one and line two. So the difference here is line two prints because it is returned from the code chunk, the code chunk, scenes line two. And since we're not like putting it in a variable or uh not trying to input it as some input to a function. But again, we'll see those in a couple of notebooks. Um Since we're not doing that, it just sees this and it's like, OK, well, that's the output line two. Now, since we want to actually print it, Uh we're going to call the print function and it prints both line one and line two. And you can tell the difference between a printed text and text that's returned as an output as a string object because the string object will be surrounded in either single or double quotes, whereas the printed text, unless it has been typed into that text, uh will not have the quotes around it. OK? Uh So you can print multiple strings with spaces between those strings if you place them in print and separate by commas. So for instance, we have print string one and string two. And so string one is a single string, object comma string. Uh The second string is the string and, and then comma the third string is actually string two. OK? And notice when they're printed out, they're all separated by spaces but no spaces have been typed within the string. ... OK. So concatenation. So concatenation is the process of combining two or more strings together. And you can do this very simply with a plus. So if you call for a plus symbol, you're going to take the first string on the left of the plus and add it to the second string on the right of the plus. So here I will do a concatenation of the following strings. Thing one. Uh and with spaces on either side and thing two. So what this will do is it takes this and says this is my first string. I will then add onto it, the string, space and space and then I will add on to that the string thing too. And then I'm gonna print out the result we could have alternatively if we were only interested in printing it and not you know the string object. If we just wanted to print this, we could have also done what we did above. Sometimes you'll need to take something that isn't necessarily a string and then cast it as a string, perhaps to write it down in your computer or to print out the results of some numeric process. Uh You can do that just like you did with in, you just call string S T R parentheses and put whatever you'd like to make a string in the parentheses. So for instance, here we have, we're gonna have say the average is and then S T R two, OK. And then for printing purposes, if you only want to print the output, uh you don't necessarily even have to cast that two as a string, you could just use a comma. So print is pretty flexible in the sense that as long as everything within each comma separated place is of the same type, it will print it. So what I mean here is here, there's a string and then a comma denoting that there's going to be a second object. Uh And then an integer. So as long as this is an in as long as uh this is an integer and not like a mixed type like plus, let's say I'm trying to concatenate an integer with a string, ... string or string, I'll get an error here because you cannot ate an integer with a string. But since it's just an integer on its own. Uh I can do this. OK. So print is pretty flexible, you can print together strings and different numeric types as long as you separate them by comments, the thing that we're gonna end on uh are built-in string functions. So Python strings are a very uh useful data type. And Python, they're used quite a bit in things like natural language processing. Uh So Python because of that has a lot of built in string functions. Uh not because of natural language processing, but because there's a lot of things you might want to do with strings. And so Python has built in a number of those functions for you that will make your life easier. So for instance, what we're gonna do and I've already done this, but in your version of the notebook, this will be empty. You're gonna put your name here in the string. That's just uh two quote marks. Uh For me, it's Matt Osborne, uh first name shortened to Matt and then you have dot Lower. And so what this does when I run it is, it's gonna take all of the characters in here. And if it's possible take it from a capital letter, the capital version to the lowercase version. So the capital version of M is lowered to the lower case version, the capital version of O is lowered to the lower case version. Similarly, there's something called upper which will do the inverse. So now everything's all capitals. Uh Another fun function is the replace. So sometimes you'll have a string where there's a lot of a certain character or phrase uh that you need to replace. Maybe you're cleaning up a string for a natural language processing task. Um Here we're gonna have an example where I have a lot of commas in this sentence. And what replace is going to do is it's going to take in those commas. And then you can notice that the second argument to replace is this exclamation point. So it takes dot replace takes the first argument and replaces it with the second argument. So all those commas now have turned into exclamation points. OK? Uh You can also go back from strings to a numeric type using float or in. So for instance, and again, try this on your own first, maybe pause the video and come back or just listen along as I do it. Uh so what happens if I put the string 2.5 inside a float ... it comes back as a float and we can even check the type of that. See it's a float. So float and in will do uh their best to take a string. And if it recognizes it as the as the uh if it recognizes the items inside the string object to be something that could be placed as a float or an integer, it will do that to the best of its ability but we can see there's a limitation to this. So what if we tried the same thing? Uh but instead of float, we put in now, remember integer to the float 2.5 would take that and turn it into a two. And so maybe what we're expecting to happen would be that it's gonna look at this, recognize that there's a numeric inside this string, which is the float 2.5 and then take that 2.5 and round it to a two. But instead we get uh an error. And what it's saying is that you cannot make an integer uh with base 10 out of 2.5 uh the string. So, you know, it, it's nice. It can take a string of a number and turn it into the number uh that it represents if uh you put in outside the right type um function. But if you put outside the incorrect type function, you're just going to get an error. OK? So that's a quick introduction to Python strings. Uh You know what a string is, you know how to print them. Uh You know, about a couple of special characters uh and you know about concatenation and a few built-in string functions. Uh So in the next video, we're gonna learn a little bit more about some data types in Python. I hope you enjoyed this video and I will see you next time. Have a great rest of your day.