Loading...
Hello, my name is Mrs. Jones, and I'm really pleased that you're here to learn with me today.
Today's lesson is called String Handling and we're going to look at how to use different string handling techniques and how to iterate through a string and use these in a range of programmes.
So let's get started.
Welcome to today's lesson, String Handling from the unit Programming Strings and Lists.
By the end of this lesson, you'll be able to use string handling techniques to find out things about a string.
There are three key words to today's lesson.
String handling.
String handling is how programmes manipulate text through operations like joining, searching and formatting.
Index.
Index is the location of items or elements in a list, array or string.
For loop.
A for loop is an iterative statement that will repeat for the length of a given sequence.
There are two sections to today's lesson.
The first is use string handling techniques, and the second is use a for loop to iterate through a string.
So let's start with use string handling techniques.
A string is a data type.
A string is a collection of characters, which can include spaces, punctuation, numeric digits, and any other symbols including the dollar sign, the am sign, and the pound sign.
In Python, we represent a string by placing quote marks around the characters.
For example, name equals, and inside the quote marks, we have Laura.
Or password equals, and inside the quote marks, we have Password1%.
Note if a string contains numeric digits, these will be handled as text characters, not numbers.
Let's have a quick check.
A string cannot hold numerical values.
Is that true or false? Pause the video to consider your answer and then we'll check.
Let's check your answer.
The answer was false.
A string can hold numerical values, but these will be handled as text characters, not numbers.
Well done if you got that correct.
String handling refers to the way a computer programme works with text, known as strings.
Programming languages provide string handling functions to make it easier to manipulate text.
For example, to search for or change a character within a string.
Common string handling operations include the length where we can find out how many characters are in a string, joining where we can add two or more strings together.
Substring about extracting part of a string.
Case conversion, changing the text to uppercase or lowercase.
And searching, finding a letter or word inside a string.
Sofia is saying string handling is used to check if your password is strong enough.
There are some useful Python functions that can be used for string handling.
For example, the len function.
And you can see here, we have len with open and closed brackets and that's important for the syntax.
The len function returns the length, the number of characters of a string.
So len will return the number of characters in a string including its spaces and punctuation.
So we have on the right here language equals and inside those quote marks, we have Python, length equals len, and inside the brackets we have the variable name.
And then below another example, we've got subject equals and inside the quote marks, we have computer science with the exclamation mark and length2 equals len, and inside there we have the variable name subject.
The first one, the length of that string, is six, because there are six characters inside the quote marks.
The second one would return 17, because remember it is that space as well that needs to be counted.
There are eight characters in Computer and there is eight characters in Science! Eight and eight is 16, but we need to remember that there's that space in the middle.
So we've also got another one, so it is 17.
Let's have a quick check, what will be printed when this programme is run? We have my_string equals 99 Red Balloons! inside quote marks, and there's our string.
Length equals len, my_string.
And print length.
So when that programme is run, what will be the outcome? Is it A, 12, B, 14, or C, 16? Pause the video to consider your answer and then we'll check.
Let's check your answer.
The answer was 16.
You count all the characters inside the quote marks including the spaces and the symbols and the characters, you get there are 16.
Well done if you got that correct.
Individual characters from a string can be accessed using their index.
The index is shown above the string here.
The index is the numerical position of the character in a string.
So the string Python has each letter, each character has an index position.
Indexing always starts from zero in Python and we use square brackets after the string to specify the index.
So this first one, print my_string and we have zero inside the square brackets will output the letter P, because P is that position, the index of P is zero.
The second print statement has the number three inside the square brackets, which if you look at the string, would be the letter H.
And the final one, the number five, would be the letter N.
The last letter in the word is the length of the word minus one.
Python also has a feature where you can use minus one as the index of the last letter.
Not all languages support this.
My_string equals Python in the quote marks, but if you look at the last one, we've got print my_string, and then in the brackets, we've got minus one.
You can see that it does exactly the same as five.
Both return the last letter.
The index out of range is an error which occurs if the index supplied is greater than the length of the string minus one.
So you can see in this example here that Python is used again, which has got five positions, five index.
But when we put seven in, it's saying an index error, string index out of range on line two.
Let's have a quick check.
How would you print out the third character from the string stored in the variable my_string? Is it A, print? And then in brackets my string in the square brackets three.
Is it B, print my_string and then in the square bracket two? Is it C, print my_string in brackets three? Or is it D, my_string and then in brackets two? Pause the video to consider your answer and then we'll check.
Let's check your answer.
The answer was B.
Remember that the positioning starts at zero and also that it should have square brackets around it.
Well done if you got that correct.
Let's do an activity.
You'll need your worksheet and it is asking you to write a Python programme to ask the user to input a string.
Use the function len, open closed brackets and an if statement to check if the string is longer than seven characters.
If it is, then print out valid.
Otherwise print invalid.
Pause the video and have a go at creating that programme and then we'll go through the solution.
Let's check your answer.
The programme should look like this.
We have my_string equals input and enter a string.
We have length equals len and my_string inside the brackets If length is greater than seven, colon, print valid, else print invalid.
Well done if you've got that correct.
Let's look at another activity.
This time, write a Python programme that asks the user to input a number.
Add an if statement to check if the number is too large to be an index for the secret word.
If it is, print out number is too big.
Otherwise print out that character from the secret string.
And you're given a start here of secret_word equals rainbow.
Pause the video to create your programme and then we'll go through the solution.
Let's check your answer.
The solution underneath secret_word equals rainbow is print enter a number or and num equals input and the brackets.
If num is greater than or equal to the length of the secret word, then print number is too big.
Else, print secret_word and inside the brackets, the variable num.
Well done if you got that correct.
Let's have a look at another activity.
And this time, I want you to extend the programme to give the user a clue about what the secret word may be.
Add a second print statement inside the if statement that displays the first character of the secret word to the user.
Pause the video and have a go at extending your programme and then we'll go through the solution.
Let's check your answer.
So here, we've got the same as before, but inside the if statement, we now have another print statement.
And inside here, we have print and then we're using the formatting.
So the first letter of the secret word is, then we've got the secret word and in the square brackets, the zero.
Well done if you got that correct.
Let's move on to use a for loop to iterate through a string.
For loops are convenient for iterating over the characters of a string, and the structure is for variable in string and then indented underneath is the block of statements.
And Andeep says, "Remember, for loops can be used for iterating over any sequence of events.
Example, a number sequence, et cetera." For example, this programme prints all the letters in my_word, in the variable my_word, one after the other.
The letter variable is being assigned to each character in turn inside the for loop.
You can see here we have my_word equals sheep inside the quote marks.
For letter in my_word, print letter.
Using a variable letter makes sense and can make it easier to make that link when we're creating the programme.
And the output on the bottom there you can see is sheep.
Each letter, each character in that string is output vertically.
Let's have a quick check.
What will be printed out after running the following Python programme? We have count_e as the variable equals zero.
My_word equals sheep.
For letter in my word, if letter matches E, count_e equals count_e plus one.
Print count_e.
For that output, A, one, B, 2, or C, count_e.
Pause the video to consider your answer and then we'll check.
Let's check your answer.
The answer was two.
Well done if you got that correct.
You can also use a for loop with an index number that changes on each iteration.
For index in range and inside the brackets, start, comma, end, comma, step.
This can work well with strings to allow you to access different characters based on this index.
Alex is saying, "Remind me of how the range function works.
What does start, stop, and end mean?" The range function returns a list of numbers within a specific range.
Here are some examples of how range can be used.
And what we've got in the first row is range and inside the brackets the number five.
And that will output 0, 1, 2, 3, 4, and it will pass one value and it will be used as the end point.
The number inside the brackets is the end point, how many iterations it will run.
Range three, comma, 10.
You can see in the what it will return section is 3, 4, 5, 6, 7, 8, 9.
It passes two values now into the for loop and they will be used as the start and end points.
It will start at position three, iteration three, and then will continue till the 10th iteration.
In the last row, we've got range, one, comma, 11, comma, two, and this will return 1, 3, 5, 7, 9.
And this time, we've got it passing three values and they are used as the start, the end, and the increment, which is the step.
So it starts at one and it will continue to 11, but it will hit step between in twos.
"This table helps me understand the range function.
Thanks," says Alex.
For example, this programme prints all the letters in my_word one after the other.
So we've got the I variable is being assigned to the values generated by range five in each loop.
For example, 0, 1, 2, 3, 4.
The I variable is then used as the index inside the square brackets to access the character at that position using print inside the brackets, my_word and in the square brackets, I.
Another example, this programme prints the last two letters in my_word, one after the other.
The I variable is being assigned to the values generated by the range three, comma, five in each loop, which is three, four, which starts from three and goes up two, but not including five.
The I variable is now used as the index inside square brackets to access the character at that position using print, my_word, and inside the square brackets, I.
And you can see the text output down on the left and just output e and p.
Another example is this programme prints every other letter in my_word one after the other.
The I variable is being assigned to the values generated by the range 0, 5, 2 in each loop.
For example here, it'll be 0, 2, 4, and that's the index position in the string.
So it starts from zero, goes up to, but not including five with a step of two.
The I variable is now used as the index inside the square brackets to access the character at that position using the print my_word and in the square brackets, I.
And you can see the text output on the bottom left where we have s, e, p, which is positions zero, two and four.
Let's have a quick check.
What could you replace the five with to ensure this programme works for any length of strings stored in my_word? The five is located in the range brackets in that programme there.
So what could we replace that with? Is it A, len, brackets and then the quote marks sheep, B, len, square brackets, my_word, C, len, brackets, my_word.
Pause the video to consider your answer and then we'll go through the answer.
Let's check your answer.
The answer was C.
Len, brackets, my_word.
And it would look like this.
For I in range, and then in the brackets, we have len, brackets, my_word.
Note the two sets of brackets, 'cause the syntax is important.
Let's have a look at an activity.
Modify this programme so that it asks the user for a character to count.
The programme should print how many matching characters are in the secret word.
There needs to be a prompt, the user, enter a character.
The user enters a character, which is stored in a variable.
Loop through the characters stored in the secret word.
If the character matches the user's character, then increment the count variable.
Print the count at the end of the programme.
Pause the video, use your worksheet, and have a go at creating that programme by modifying the one that's been given to you.
And then we'll go through the solution.
Let's have a look at the solution.
This solution, we have count equals zero, secret_word equals sheep.
Print, enter a character, user_letter equals input.
For letter in secret_word, if letter matches user_letter, then count equals count plus one, and then we've got print with the format.
The letter, user_letter appears count times.
Well done if you've got that correct.
Let's have a look at another activity.
Create a Python programme that compares two words entered by the user and checks which letters match the same index.
The programme will display whether each pair of letters is a match or no match.
A, ask the user to enter two words of the same length.
B, if the words are not the same length, print an error message, C, use a for loop to compare each letter in both words, one by one.
D, if the letters at the same index match, print, match tick.
E, if they do not match, print no match cross.
And F, add a score that increments for each matched letter and display the score at the end.
Pause the video and use your worksheet and have a go at creating that Python programme using all of those areas.
And then we'll go through the answer.
Let's check the solution.
Here is the solution programme.
We have word one equals input, enter the first word.
Word two equals input, enter the second word.
If length of word one is not equal to length of word two, then print error, words must be the same length.
Else, print comparing letters.
Score equals zero.
For I in range length word one, if word one position I matches word two, position I, print using the format word one position I and word two position I, match tick.
Score equals score plus one.
Else print, use the format word one position I and word two position I is no match, cross.
Print in the format final score, score out of len, word one.
Well done if you've got that correct.
In summary, a string is a collection of characters.
String handling uses techniques and built-in functions to find out information about strings.
The len function is used to return the length of a string.
The index number is used to retrieve a single character from a specified position in a string.
For loops can be used to iterate through some or all of the items in a string.
Well done for completing this lesson on string handling.