video

Lesson video

In progress...

Loading...

Hello and welcome to lesson two of our Programming Part 5:Strings and Lists unit.

I'm Ben, your computing teacher for this lesson.

So, in this lesson, we're going to look at String Handling.

Okay, now, you may notice it says String Handling 1, which means this is one of a sequence of three lessons, where we're really going to explore all the different operations and things you can do with Strings.

Okay, so, all you'll need for this lesson is a computer, a web browser, and you're also need access to your replica account, so, please do ask your parents or carers for permission before accessing that account.

Okay, it's always really helpful as well to have a pen and paper at the ready, just in case we need to make notes as we go along.

Okay, so, if you can clear away any distractions that you might have, and when you're ready, let's get started.

Okay, so, specifically in this lesson, you will describe the function of string operators, you're going to be able to use string handling techniques and we're going to remind ourselves about for-loops, and how we can use those with string operations.

So, let's start off by making a prediction, you got two lines of code there on your screen so, I got a question for you, and that is, "What will be the output of print when this programme is executed?" So, you have three options, four options, sorry, you have a Python times three, Python, Python, Python, but with a space in between each string, or Python, Python, Python with them altogether, or there'll be an error in the programme, cause you can not multiply by a string.

So, what do you think? Okay, so I'm going to give three seconds, can you shout out the answer at the screen for me? Okay, so, three, two, one, it is, hopefully shouted three.

It is actually answer number three.

It is Python, Python, Python with no spaces in between.

So, why is that you might be saying? Just in case you didn't get the right answer, and because you can multiply string believe it or not, but it will concatenate the same value for the number of times stated.

So, concatenate means joined together.

So, there won't be any gaps made, for example if you were to, not that you could do this with this, but if you've ever used a string and use commas before, then it separates the values, whereas a plus joins them.

So, essentially what we're doing we are concatenating them so, we're joining the string together what it's doing is literally printing out the word Python three times, but joining them together each time.

So, another question for you then, so, this is slightly different, so we put and compare Python is greater than Java, okay? So, this isn't an opinion thing by the way, this is just how would Python handle this? So, what would be the output print when the programme is executed? Would it be one, two, three, or four? So one true, two false, three, it's not possible to know the output without executing the programme, or four, there is an error in the programme.

So, what do you think? So again, shout out to the screen one, two, three, four.

So it is, three, two, one, it is, number one is true, and believe it or not, it is true.

And again, it's not opinion, it's not saying that, well, of course, Python's going to think Python's greater than Java, okay? But actually, if you imagine these words are in a dictionary, Java would appear before Python so the J would appear before the P, and this makes Python greater than Java.

So, actually that would appear as true.

Right, next one then, so, this what we're going to do, is going to put Python plus Java, so, what would be the output of print when this programme is executed? Is it one, Python plus Java, two, Python Java, with a space, three, Python Java without a space.

or four, there's an error in the programme cause you cannot add two strings together.

What do you think? Hopefully, if you remember the answer to question one, this should help you.

Okay, so it is, three, two, one, it is number three, again, we're concatenating here, we're joining them together.

So, the plus sign concatenate, so it joins the two strings together, there's no space, as we haven't given them the instruction to do so.

So, what we've done there, if you perform some operations on a string, and we can perform operations in a string in a similar way to the operations that we can perform with numbers.

So, we can also use string handling techniques to find out things about a string.

For example, again, give you multiple choice.

What do you think the output of this programme will be? So, word equals Python, word length equals len, in brackets word and then print word length.

What do you think? Well, actually the output would be six.

So, why is that? Okay, well, the len function we're using here, is used to count the number of characters in a string.

And it returns a number, so, it is a function, cause it returns something, and this would include spaces as well if they're present.

So, what we're doing is if we look at this code, we're defining a string, we've got word that has another variable that I have decided upon and we're using the Len function, and we're passing into it, that parameter there, so, we're passing into word, which is our string.

So, we're passing into it, Python.

So, len is going to return back to me, the number of characters in that string and that does include space.

So, if there was a space between P and Y, for example, that will return seven.

So, we can also use string handling to access a character of a string.

So, what do you think this programme will output? So, with word equals Python, first letter equals word, square bracket zero, and then print in brackets first letter.

So, what's this going to output? What do you think? Well, the answer is P.

So, why is it P and what does zero mean? Well, each character string has an index position, okay.

Now, in Python, an index always starts by default at zero.

Okay, so we were using the word Python here, you can see we've got P's at positions zero, Y as a position one, two at position two, H is position three, and O at position four, and N is position five.

So, even though there's six characters in the string, we start counting the index position from position zero, Okay, so what would we need to write in order to access the position T in Python? Alright, so what we'll do is write word and then we put something in square brackets there.

What would we put inside the square brackets? Okay, well, we would put in, the number two, inside those brackets so, we put in word in the square brackets, two, it looks for the index position, okay, in the string, so zero, one, two comes to T, zero, one, two, and that's T.

So, that would output T if we were to put the print in front of it, okay.

So, what I'd like you to do now, is to create a programme that uses these techniques, okay.

Now, this is going to build on a lot of the programme knowledge you built off the previous four units, but we're also going to add in some of those, the string handling techniques that we just learned about that, okay, so, what I'd like to do is use previous programmes and the example code given to create "A guess the word games".

The idea is you've got to get the user playing the game got to guess the word.

So, I'd like to head to Task 1 on your worksheet and that should help you complete the activity, so, I'd like to pause the video now, and go over to that task sheet and have a go at that programme.

Okay, have fun, good luck and I'm looking forward to seeing your solutions as well.

So, unpause the video when you're done.

Okay, so how did you get on with that? Okay, so what I'm going to do, is show you the example solution here, okay, and this is my example solution.

I won't go through line by line, but if you're struggling at any point, you might want to pause the video and just have a look at this code and then maybe minimise the screen, go to your Python work and see if you can have a go yourself.

What's important is you don't just copy this, without trying to understand that, okay, so, I'm just going to quickly run through some parts of this, and some key parts, right.

So, the first block of code, you can see at the top here, we got word equals sandwich we're setting out right from the variables it's going to store the string of the word that we want to guess.

Okay, then we've got empty string here, which is going to be the user's guess.

Now, this is an interesting bit here, I wanted to describe to you.

So, I've got a variable here called not guessed.

Okay, and that's equal to this.

Now, that says, "Word is not equal to user guess".

So, that's going to return to me a Boolean value, it's going to be about to return true or false.

So, what we're saying is, word is not equal to user guess.

So, is word equal to user guess? So, sandwich equal to an empty string? No, it's not.

So, because we got, not equal to that, this means this will turn true.

So, currently the value being stored, by the variable, not guessed, is actually true, okay, then we got a variable that's going to have the count or store the number of guesses, right, we can use to increment its size.

So, initialising that variable where we can increment the number of guesses.

But here's the bit where I wanted to describe to you, so, while not guessed, okay, so, it's actually saying, while that is true, and it is currently true, and guesses is less than five because we allow a maximum of five.

Then we run through our programme, okay, and we've got, if guess is equal to one, then first letter is word position zero, and we're going to say, the first letter of the word is, first letter.

So, we're assuming that it gets the wording correctly first time, so, when they get letter one, we're going to show them the first letter, okay, then we going to give them another guess, if it gets to three guesses, we then get to show them the last letter of the word so word, at position zero, so seven, sorry, which is the last letter in the word, okay, and then we can print guess word, and then we can say user guesses equals input, and they increment the guesses, okay, and then check if they're no guesses that equal true or false, okay, then in the end of the guess the word, and we're going to say, "Correct," you win, otherwise it's going to say, "Incorrect," you lose.

Okay, so, that's a basic way in which we can do.

There are other ways in which we can enhance this programme but we'll look at that in just a moment, okay, so, like I said, if you didn't get to the solution, do pauses, minimise it, don't copy and paste and see if you can work out from what I've just said, what the solution will be, okay.

Right, so I'll move on, okay, so, let's have a look at for-loops, iterating through a string.

Now, what a for-loop can do to iterate is useful, something called a for-each-loop, right, so, we can do, for variable in string, okay and then my block of statements.

So, for-loops are really convenient for iterating over the characters of a string.

So, remember, for-loops can be used for iterating over any sequence of elements, not just like a number sequence.

So, if we look at here, this a block of code here, what do you think will be displayed on screen when this programme is executed? So, draw upon knowledge of for-loops, to see what you think the answer would be, okay, Well, the answer was sheep, but notice how it goes down like this, so what's happening is, we're printing a letter, move into the next line printing another letter.

So, each time we iterate through that string, we're performing that function there, where we're printing the letter.

So, for each letter in word, it's going to print the letter.

So, when that is this by default, this variable that we created here is going to be zero, so, it's going to be zero, so, it's going to print S.

That's going to move through it, so, letter becomes one, so, then it's going to print H, then letter becomes two, it's going to print E and so on.

So, what do you think would be displayed on the screen when this programme is executed? So, I'll give you just a moment to read that code, and see what you think would be executed.

Okay, so it's word equals sheep, I've got a variable count_e equals zero, and then for letter in word, if letter equals E then count_e equals count_e plus one, the end print count_e.

Okay, so, hopefully just by reading the variable you have good indication about what this is meant to do, but actually it's going to output two because the count variable is initialised to zero and it's incremented one time each time E is found in that string.

So, it's iterating through all the string but each time it finds E then it just increments that variable count_e and then prints it at the end.

So, let's walk through that, to make it really clear, sorry.

Okay, so currently the word is sheep.

So, what happens is an account is being initialised and count_e equals zero.

So, we're going to look through that first letter in the words, so, for letter in word, okay, so I'm looking at S, I'm going to say if letter equals E is false so it doesn't execute that line underneath it, so, then we go through the loop again.

So, for letter in word, so we're now looking at H, if letter equals E, which it doesn't, okay, that equals false, so, we go through it again, so, then we get and that actually equals true! So, because it equals true, you'll notice my count_e variable increments by one, okay, let's move to the next letter, that is E.

So, if letter is E that is true, okay, so, it goes through to the next line, and it increments count E by two, okay, then we go for the next letter in the word, which is P and that is false, so, that doesn't execute the next line, we get to the next one, and look, we notice we've got to the end of the word now, so it knows to terminate the loop, okay, and then to break out of it and go down to the next line of code, which has a print count_e which has two, okay, so, now we know that, what I'd like you to do, is go Task 2 on your worksheet to modify the programme we've just looked at.

And that should incorporate this code into the guess the word game.

So, the code that we just looked at, showed you how to iterate through strings, I'd like to, then follow the instructions on Task 2, which will tell you to go back to your guess the word game, and add a loop into that, okay, so, good luck with that, and then unpause when you're done.

Okay, so now we've done that, let's have a look at this one then, okay, so, this is the guess the word game, so it is modifying it, so, I've got my word equal sheep, count equals zero, enter a character, okay, it's going to store that input onto a variable character, and then we're going to go through each line, each character and it increments every time, and it's going to tell me how many times that letter appears in the word, okay.

So, final attempt for this lesson, is to look at some Python documentation.

Now, I'm a big fan of looking at telling people or myself as well doing it, "Look at Python documentation".

Okay, Python docs exists and shows you, all different operations we can carry out, on different things that are built inside Python.

Okay, and looking at documentation is such an important skill, okay, cause you're never going to know what the functions do or how they can be used unless we look it up.

So, what I'd like you to do is get some practise doing that, and I'd like to discover what the following string methods do, okay, so, string capitalise, string title, and string swap case.

So, I'd like to head over to Task 3 on your worksheet, where there's a link to development Python docs, where you've got to look through it and see if you can work out what those three things do, okay, so, pause the video and then unpause when you've done that.

Okay, so let's now go through the answers for that then, so, string capitalise, what was that meant to do? Okay, well, this returns the string or the first character that is capitalised, okay.

String title returns the string, with the first letter of each word that is capitalised, okay so, it looks at all of it and just returns the first letter of the word that's capitalised, okay, and swapcase, swaps the case, so lower case becomes upper case, but if it is upper case, it will swap to lower case.

Okay, so hopefully when you're looking that, you saw a whole bunch of other things that we can do on strings, okay, and hopefully, it opened your eyes to all the tonnes more things that you can do on strings, and if you ever think to yourself, "Oh, I wonder if you can do that, "that'd be helpful.

Then look it up, and the chances are, somebody's made a function for you that will do the former task that you needed to do.

So, that is all I have for this lesson, and hope you've enjoyed getting started with string handling, okay, So, next lesson we will continue looking at string handling and take a look at more things we can do, okay.

Now, I would absolutely love to see your work, I'd particularly like to play your guess the word games, okay, so if you've got a guess the word game you'd like to share with me, then I'd take a print screen of it, or share a short link somewhere on Instagram, Facebook, or Twitter, tagging up Oak National, and using the #LearnwithOak and don't forget to ask your parents or carers to do that for you.

Okay, so, I'm already looking forward to that class and I hope you are too, and I'll see you then.