Posted on

String manipulation in Python

Enroll in The Complete Python 3 Course: Beginner to Advanced!

Click here to subscribe for more videos like this!

Alright, so what are some of the fun things and useful things that we can do with strings in Python? Well there’s a few concepts that I want to talk about here, so what we’re going to do is go ahead and open up a Python interpreter, you can do this in pycharm by going to view tool windows, and then selecting python console, or you can open up terminal and type Python3 and hit enter. However you enter this if you want to follow along that’s probably helpful because I find if you’re told how to do something you know you kind of know how to do it, but if you actually do it yourself you have a better handle on what happens, and you’re more likely to remember how it works. So from here on out basically what I want you guys to do is to follow along with your code and do everything that I do. So, with that being said one of the first things that we’re going to do is in a previous video when we were talking about numbers I showed you how to convert strings into numbers if the only thing in that string were numbers. So, this is going to be kind of the opposite. Let’s say you have a few values, let’s say you have the number 6, right, and you want to print out this costs six dollars. You can’t, just well actually first we’re going to talk about concatenating, because you can’t do that in Python. I’m going to show you how, but first let’s learn how to concatenate a string which means had a glue multiple strings together. So we saw this actually in the numbers video as well when we tried to add numbers that had been identified as the string or yeah the string type and what it did was concatenate the numbers and made 5 and 6, when you add them together, it made it equal 56, and so your gluing strings together. So I want to say “Hello, ” then pop out of that string put a “+” and then in this string I’m going to type “Nick” Now what’s going to happen is it’s going to print out “Hello Nick” because I’m concatenating these two strings. You can also do this with however many instances of strings you want, so if I really wanted to get a little bit convoluted I could do this and this would print out hello using five strings all concatenated together. Now what happens if I want to say this costs six bucks? “This costs” + 6 + “bucks” Well, because part of this line is an integer it’s going to try and use those plus symbols to perform a mathematical operation rather than printing out a string, actually it’s the opposite it can’t convert the integer to a string so it knows that we want to concatenate some strings together but it’s like well hey hold up this is not a string I don’t know what to do with this. So what we have to do is use the string function which is similar to the int function. So how we do that is “this costs + str(six) + “dollars” and what this is gonna do is it’s going to convert the number 6 to a string so it can be used. Now you can also perform mathematical operations within the string parameter, I believe so. Let’s test this out. “This costs” + let’s go str(6 + 5) + “dollars” and hit enter. Yeah, so it can perform mathematical operations within that parameter. It’s going to convert the sum or the result of whatever mathematical operation you are performing on it, it’s going to convert that to a string and then use that string to be concatenated with the other strings that you’re working with. So that’s how to use the string function and maybe this doesn’t seem like it’s going to be important, or maybe it does but you’re like well how often do you use this, you know. We’re going to see because when we get into defining some functions we’re going to be using both strings and integers to return certain information and so this is going to be important at that point. So, that’s how to convert integers to strings, it will also work for any other type. So if you have true, we’re going to get into that after actually, but we’re going to be using this string function again at a later point. Now what happens when you want to do the opposite of concatenating strings? So, think of concatenation, I’m not sure if that’s the appropriate term, think about contaminating two strings as actually let’s look that up. Now it’s gonna open up xcode just go in to Google here, because if I’m saying that wrong I want to know. Yeah, okay, concatenate, good stuff. Not sure if you guys heard that but I’m using that word correctly, good stuff. Alright, so anyway back to the code that’s how you concatenate two strings together. What happens when you want to do the opposite, you want to split strings apart? So, let’s say we have this string like this so we’re going to say “Hello:Nick” we’re going to separate that word from the other word with a colon so that we actually have a character that we can use to split. So what we’re going to do is after the string we’re going to put a “.split(“:”)” going to call a function split on it, and the split function or method takes one parameter, well for the sake of this video takes one parameter, and that’s where you want the string to be split. So we’re going to split it at the colon there and hit enter. What it’s going to do is it’s going to create an array with all elements that had been split from that point, so for instance it split it into two items in the array but let’s say I did this it’s going to have an array with three values in it, and then we can reference those values by its index number, so this is going to be a bit more complicated. Let’s say we’re splitting it at this so let’s concatenate that lets say “My name is” + so we want this to print out my name, and not hello world, and not anything else, so what we’re going to do is put 2 square brackets and the number one “[1]” and it’s going to say “My name is Nick” I’m not sure if that’s a bit too complicated, so I’m going to try and explain it again from the beginning. So, we have a string, we have it say my name is and then a space, and then we can concatenate it with the result of this right here. So this can be broken down into a few parts, here’s the string that we’re performing the split function on okay, then we’re calling split on it and this is going to turn this string into an array with three values each with an index. It’s gonna split it into hello, Nick, and world and stuff those values into its own array, and then we can access the result of this by using an index number here in square brackets so we’re calling the index number one, so hello would be 0, Nick would be 1, and world would be 2. So, what’s happening is it’s printing out my name is Nick. Now you might be thinking shouldn’t it be printing out my name is hello if we’re using the number one here? This isn’t so much Python but it’s a concept in programming in general, no. So, 0 equals 1. If you have an array the first item in it you think would equal 1 but it actually it equals 0 and we’ll talk more about arrays, which are actually called lists in Python, we will talk more about those coming up in you know I think not the next video but the video after the next one. So that’s how you can manipulate strings in a very basic way, we might expand on this in the future, we actually probably will. So, next we’re going to be talking about Booleans.