4. Working with strings in Python
Oct 9, 2023
Strings are ways that we can store text in variables in python. We cover how to use strings, how to access characters within strings, slicing, useful functions like len() and str() as well as useful methods like .upper(), .replace(), split() and more.
All right, we're now ready to dive into strings strings and Python are awesome
They're pretty easy to work with right first thing you should do is we should open up spider in spider
We're gonna declare a string. Let's declare the name of my dog
So we're gonna choose a variable the name of that variable is dog and the value that we're gonna store in that variable is
Scout the name of my dog
Now you see here. I used single quotation marks, but we could have just as easily done double quotation marks
Either of these will work right so essentially what we have is one command saying to name Scout
The variable dog Scout and then we do the exact same thing afterwards
So if we run it, it's still just going to produce one variable and its name is going to be Scout. Okay straightforward enough
Next it says you can assign multi-line string if you use three double or single quotations at the beginning in the end
so we could say a poem is equal to let's do three single quotes and then you say the
Happy dog ate all his food just like that and when I run this
You'll see that right here
This poem keeps those spaces in it right in fact if we could print this we could print the poem
Let's go print and we're gonna type in poem because that's the name of the variable holding this string
So if we since print is expecting a string and poem is a string type variable it should run this no problem
So we print it and sure enough the happy dog ate all his food, so it kept those
Those spaces right those we call those carriage returns. They're new lines, okay
We could have just as easily done this with
The double quotation marks by putting three of those in a row that would have also been allowed
Okay, what else do we got says strings are arrays and the elements of the array can be accessed with square brackets
So what does that mean well a string is a bunch of characters right what this means is that we can access
Individual characters starting with zero Python starts counting from zero don't ask me. I didn't make that rule
That's what they do so when we do this dog zero
That's going to access the character at the zeroth position right the very first position
So if we come down here and type that in here to my console and we hit out there
It's going to return capital s because that's the first character right we could do it
To the next character by typing in one there right so it's if you hit up. That's gonna bring our previous thing now
It's gonna be one and you see that it is SC and we could keep on going in fact
We could go backwards too. We could say the negative one. That's gonna return the one one back from the end right
So there we go negative one we could do it negative two and that's gonna give us you and so forth right so that is how
These indexes worked okay. We'll talk a little bit more about those when we get to slicing in a minute now
We're not able to change an individual value for example if I say dog
at the zero position is equal to
T
It gives me an error because strings are not changeable. We can't change the string right we can grab chunks of letters at a time
Instead of grabbing a single letter we can grab chunk. This is called slicing so to do this
We're gonna take dog the name of our variable and now I'm going to put the colon there that means take everything
from the start all the way up to
The third one third variable, so that's going to include the zeroth and this the first letter in the second letter
But not to the one in possession three it's gonna go up to position three so when we run that the zero position
The one position and the two position it does not deliver the three position, okay?
Or we could go the other way like let's do this
Let's do dog and we're gonna go give me the letters from three until the end right so now it will start with three
So it's gonna give us U and T right see how this make see how this works, and you could pick any number in between
Right if we go from position one up to four then it goes it skips the zero and it stops at four all right
So that is slicing we will do a lot more slicing this class because you use that for lots of things
But that's the idea behind slicing
Let's see you can get the length of a string using the function len right so
Length will tell us how many characters are in it right so before here. We want to say
Number equals the length of our let's see poem it will tell us all of the characters in poem, right?
So we can go ahead and run that when we hit f5. I'm just gonna come here number
It says that there's 30 characters in there right so there are 30 characters in this poem that we wrote okay, so that's length
You can check to see if a certain phrase or character is present in a string
By using the keyword in or not in so we can say is
S in dog and it responds true right it sends back the boolean value of truth
We could ask it if capital T in dog and it's going to say false
But if we ask lowercase T in dog it will say true
So this is case sensitive when you're using the in and the out command okay, or in and not in command, right?
If you have something that is not a string that is say a number you can convert it to a string using the str
Function so let's give do an example of this right now number is an integer right it is not a string
It's a number it has a value of 30 if we wanted to turn that into
3-0 the string we could do that we could say
We could say let's say we want to print that right we're going to print
We're going to do str. We're going to turn into a string number right so by doing so
Strict number has been turned into a string so when we hit print here
It's going to spit out the poem and then 30 right so that's turning a string something else into a string using the string str
Command if you want to intentionally add a new line, then you can do that with the backslash n right so essentially
What we're seeing here is by me hitting enter here that introduced a backslash n right?
Now you can see that if we have this
Print it out. Let's do that. We're going to type here list
List is we'll talk about lists later
It's essentially going to list all the characters out in the string right and sure enough right here
We see the happy and then you see the slash n
So that is the the carriage return between happy and dog you see the slash n. That's the new carriage
So that's not a space. It's a new line. Okay, so that's what that signal symbol means
And we've already talked about slicing so there's lots of useful methods available when working with strings for example strip
We'll remove any white space from the beginning or the end right all right
So we could do that you could do lower, and that will return the string in all lowercase right so
So we could call this variable lower dog and that equals dog, but we're going to apply the method dot lower
Right so remember a method is always applied after the variable so dog dot and then you type the method so when we run this
We're going to see a new variable over here lower dog and sure enough see the capital S is now a lowercase s
So that worked great, okay
There's upper which does the exact opposite
Upper dog you can imagine what this is going to do if we take dog and we apply to it the upper
It's going to take all of our lowercase variables and lowercase characters and make them uppercase
Let's run it and see and sure enough Scout is now all uppercase, okay?
Other great methods you can use there's replace you can replace a character with another character in a string how about this one so?
So let's rename our dog scoot instead of Scout right so what we're going to do is
We're going to call this a new name is equal to dog
But we're going to replace
We're going to replace the u
with
The o and if you didn't remember the syntax it gives you a key it gives you a tip right here
It says the old tell what the old string is so what the new string is right?
And there's even other things like you can only do it to the first number if you want like only replace the first three
Or four or five right now the way. I've got it's gonna replace everything so if we run this
We're gonna get a new dog name and sure enough Scout has become scoot. Okay. What else can we do there's split
This is a really cool thing it will split your your string up into smaller chunks, right?
So you need to tell it what you want it to split by so if you've ever worked in Excel
And you've had comma separated values. We often want to split on those commas, so let's do this
Let's split here our poem. Let's split that using the blank spaces every time that there's a space. Let's make that a new word so
How about this we're going to do?
poem
Dot split we're going to split on space on those right and now
That's going to give us back a list you see that this area here says it's going to return a list
We haven't talked a lot about lists yet
But that's essentially what we have with our our our strings strings are a list of characters
This is now going to return a list of strings right so let's have it return the first one
Right so this should be the word the if we've done it correctly, so let's test this let's try and print this line out
All right, so let's take this whole thing and let's bring it down here and see what it gives us
Yeah, sure enough it gives us the word the if we wanted it to give us the second word
Then we would put one in there, so let's hit up and then here instead of zero
Let's type one this better be the so happy this time and sure enough happy
But check this out this time it says happy
But then it has this symbol right there that character remember the backslash n means give it a new line
That's not the same thing as a space and so it didn't split on it
So we if we wanted to just have happy we would need to go through this again and split also on the backslash n
Character okay, but that's how the split and replace works, okay?
Then there's count count will tell you the number of times a given character shows up in your string so we could say this we
Could say poem we're gonna count all the times that the letter e shows up in that poem, so we're going to do
Poem count then we're gonna give it the letter so it's gonna be e I count just one
Let's do a different one that counts more than one how about a?
One two three there should be three
Right so we're gonna count three and have it give it to us and sure enough it counts that there are three of those in
There so you can see how these methods are super helpful these make it so we don't have to do nearly as much programming because
We can rely on these built-in methods that are provided for us in Python. Okay. Well what else?
Let's say a little bit more about
When you want what happens if you want to type something and it includes one of the letters that we're not allowed to use for example
a quotation mark right
What if I wanted to say this if I wanted to print this I wanted to say?
We're going to print my dog's name is Scout my dogs, so we need to do DOG apostrophe s right so how are we gonna?
Do that so we're gonna say my?
dog's
Name is and then we would do comma and then Scout look what's happened. Can you see the problem here?
We've got lots of problems. We're only allowed to put strings here, and this is a bunch of other stuff so
Something's wrong here, and we needed this to be an apostrophe there
But just one but now it's it's wrong too because this needs to be a variable name
So what we needed to do is we need to use the escape key the escape key is the backslash key followed by one of
These other characters depending on what you want it to show so we wanted to show the apostrophe
So we're gonna do a backslash apostrophe now. It's going to read out my dog's name is Scout
So let's go ahead and run that
I'm just gonna run that one section and again. I can highlight that and then hit f9 to run a single line, and it says
Scout is not defined. Do you see the problem here? I got too fast here
I told it to print out the variable Scout
But Scout is not a variable dog is the variable the value of Scout is stored into the variable dog
So I need to replace Scout with dog so let's try that again
So on this line, I'm gonna hit f9 and this time
It says my dog's name is Scout and look it included that apostrophe
So that's how you use the escape key you could make it do lots of cool things in fact
There's something else you could do. I think this will work
You can put an R in front and that means read it literally
we could do this we could do a double quotation mark a
Single and then we could do a double quotation, and I think that this will work. Let's try running that
Yeah, so what does the R do the R means take this literally the characters?
Just take them as they are you still have to something have something at the beginning in the end in this case
I use double quotations so that I could use a
Oops, that's in the wrong spot. I could use this single quotation in the middle, but
It worked just fine to say my dog's name is Scout, okay?
So that is how you do these escape keys, and that's just some of the basics of working with strings, okay?
All right, let's dive into our next video which is gonna be on booleans

