Lesson video

In progress...

Loading...

Hello, my name is Mrs. Jones.

And I'm really pleased that you are here to learn with me today.

Today's lesson is called String Manipulation.

And we're going to look at how to use the random module to select items from a string randomly, and use Python string handling techniques.

So let's get started.

Welcome to today's lesson.

Today's lesson is called "String Manipulation," from the unit "Programming: Strings and Lists." And by the end of this lesson, you'll be able to create a programme using string handling techniques.

There are four key words to today's lesson.

Random.

Random is a module that can generate random numbers, pick random items, or shuffle a list.

Concatenation.

Concatenation is the process of joining strings together to create a new string.

Lower.

Lower is a string method that converts all letters in a string to lowercase.

And upper.

Upper is a string method that converts all letters in a string to uppercase.

There are two sections to today's lesson.

The first is select items from a string randomly, and the second is create a programme using string handling techniques.

So let's start with select items from a string randomly.

Validation is the process of checking an input meets certain requirements.

This is often used in checking usernames and passwords when logging in.

Here's how to create programmes to generate passwords.

And Andeep says, "Passwords should be hard to guess, and easy to remember." Using the random module lets you pick random numbers using random.

randint(start,end).

Shuffle lists using random.

shuffle(list).

Pick random characters from a string with random.

choice(string).

These techniques can be useful for creating interesting passwords.

Izzy says, "And I thought random was only used to create random numbers!" To use the random module in a programme, you need to import it before you use it.

This is done with the import random instruction, and it is the convention when programming that the import statements appear at the beginning of a programme.

If you forget to import it in your programme, you will get the following error when you try to use it.

NameError: name 'random' is not defined.

The random module can be used to choose a random character from a string using random.

choice, and inside the brackets, string.

In this example on the left we have import random.

My string equals bakewell tart.

Letter equals random.

choice(my_string), print letter.

And a random letter from that string is then output and in this case, the random letter was W.

And if you ran it again, you'd get a different one, because it is a random letter generated.

You can also use this with a list to select a random item from a list.

Let's have a quick check.

What is the missing line in this programme to print a random letter from this string? We have line one, missing.

Line two, my string equals pastry.

Line three, my letter equals random.

choice my string.

Line four, print my letter.

Is the missing line, A, import random.

B, import choice.

Or C, import random.

Pause the video to consider your answer and then we'll check it.

Let's check your answer.

The answer was C, import random.

Well done if you got that correct.

Let's have another check.

What is the missing line in this programme to print a random letter from this string? We have line one import random, line two my string equals pastry, line three is missing, and line four, print my letter.

Is the missing line A, my letter equals random.

choice my string.

B, my letter equals random.

shuffle my string.

Or C, my string equals random.

choice my letter.

Pause the video to consider your answer and then we'll check it.

Let's check your answer.

The answer was A, my letter equals random.

choice my string.

Well done if you got that correct.

Let's look at an activity and I'd like you to write a programme that asks the user to enter a string.

Randomly picks one of the letters in the string.

Asks the user to guess which letter has been picked.

If the user guess correctly output "yes".

Otherwise it says "bad luck it was" and displays the chosen letter.

Pause the video and have a go at creating that programme and then we'll go through a solution.

Let's look at the solution.

We have import random, print enter a string, my string equals input, rand char equals random.

choice my string, print guess the random letter, guess equals input, if guess is exactly equal to the rand char print yes, else print in the format bad luck it was rand char.

Well done if you got that correct.

Let's have a look at another activity and this time modify the programme so that it repeatedly asks the user to guess a letter until they guess correctly.

And there are some hints.

Create a variable called guest and initialise it to false at the start.

Use this variable to control a while loop.

Pause the video and have a go at modifying your programme and then we'll go through a solution.

Let's have a look at the solution.

In this one we have import rand, print enter a string, my string equals input, rand char equals random.

choice my string, guessed equals false, while guessed is equal to false print guess the random letter, guess equals input, if guess matches rand char print yes guess equals true.

Well done if you got that correct.

Let's move on to the second part of today's lesson.

Create a programme using string handling techniques.

Strings and substrings can be joined together to make new strings and this is called concatenation and the operator used for concatenation is the plus sign.

So in this first example we have first word equals cherry.

Second word equals drops, and a new word equals first word plus second word print new word.

And the output will put those two words together cherrydrops without a space because it is putting them together.

It's concatenating those two strings together.

In the second example we have word equals candy floss.

First three equals word [0:3] Last three word, [8:11] New word equals first three plus last three print new word.

This time we're using the index positions and we are concatenating those letters selected from each of those words to put it together in the output.

Let's have a quick check.

What would be printed out after running this programme? Word one equals hello, word two equals world.

Word three equals word two.

Position start two and end four plus word one.

Start position one, end position three and print word three.

Would it output A, RLEL.

B, LLOL.

Or C, RLDELL.

Pause the video to consider your answer and then we'll check it.

Let's check your answer.

The answer was A, RLEL.

Well done if you got that correct.

Concatenation is often used with iteration to build up a new string.

We have steps here.

Step one, before the loop starts, create a new variable and initialise it as a blank string.

And a blank string is just the quote marks with nothing inside it.

For example, my string equals just the two quote marks, nothing inside.

And then step two, inside the loop, concatenate any new string data onto the end of the string.

For example, my string equals my string plus new string.

Sophia says "It's a bit like adding to a score inside a game every time you score a point." Example score equals score plus one.

In this example, a list is iterated through and each time is concatenated to a string called my string, which is printed out in each loop.

We have my list equals cat, dog, mouse, goat.

My string is initialised and it is empty.

For item in my list, my equals my string plus item, print my string and you can see the text output is cat, catdog, catdogmouse, catdogmousegoat.

Each time it iterates through the list, it adds a new item to that string.

Alex says, "How would you add spaces between the words?" which is a very good question, and we would do that by adding, we would concatenate by adding in an empty space in between.

If you look on line four, we now have my string equals my string plus, and inside those quote marks in the middle, there is just a space, plus item which would add a space in between each item as it's added to that string, that new string that's output.

Let's have a quick check.

This programme asks users for five letters and makes a word.

What should go in the missing line? Line one says, new word equals initialised empty variable, two for I in range five, line three print enter letter, line four letter equals input, line five is missing, and line six print new word.

Is the missing line A, new line equals new line multiplied by letter.

B, new line equals new line plus I.

Or C, new line equals new line plus letter.

Pause the video to consider your answer and then we'll check it.

Let's check your answer.

The answer was C, new line equals new line plus letter.

Well done if you've got that correct.

Strings can be converted into upper or lowercase using the upper and lower methods.

And notice the syntax upper() and lower().

And in this example we have first word equals fudge.

Next word equals cake.

Print first word.

lower() and notice that the brackets around that is from the print.

So inside the print brackets is where the variable name, followed by a full stop, followed by lower() needs to be.

And the same on line four, but this time using upper print next word, full stop upper() and that output would be fudge in lowercase and cake in uppercase.

Let's have a quick check.

What would be printed out after running this programme? First name equals Laura.

Last name equals Smith.

Print first name.

lower print last name.

upper.

Is it A, Laura with a capital L and lowercase and SMITH, all capitals.

Is it B, laura all lowercase, SMITH, all capitals.

Or is it C LAURA all capitals and smith in lowercase.

Pause the video to consider your answer and then we'll check it.

Let's check your answer.

The answer was B, laura all in lowercase and SMITH all in uppercase.

Well done if you got that correct.

Let's have a look at an activity and have a go at creating a Python programme that generates and prints five unique random strings.

Each string should follow these steps.

Randomly select a vowel from a string of vowels "aeiou" and convert it to uppercase.

Randomly select a consonant from a string of consonants and convert it to lowercase.

Randomly select a special character from a predefined string for example, "!@#$%^&*()".

Concatenate the selected vowel consonant and special character into a single string.

Repeat that process five times and print each generated string using a loop.

Pause the video to have a go at creating that Python programme and then we'll go through a solution.

Let's have a look at the solution.

Import random, vowels equals "aeiou" consonants equals "bcdfghjklmnpqqrstvwxyz" special chars equals, and we have those symbols, for I in range five.

Result equals initialise empty variable at this point.

Result equals result plus random choice vowels.

upper, result equals result plus random.

choice consonance.

lower, result equals result plus random.

choice special chars, print result.

Well done if you've got that correct.

This is an example of what you should see.

Now remember, this will be random, so your text output would be different.

It gives an example of the kind of way it might look.

Each would have three capital, a lowercase, and a symbol.

In summary, the random module can be used to generate random numbers and pick random letters from a string.

Concatenation is a way of joining strings using the plus operator to make a new string.

Using random and concatenation together in a programme is a way of generating new random strings that can be used for strong passwords.

Strings can also be manipulated using the upper and lower string methods to convert the string into upper or lowercase.

Well done for completing this lesson on string manipulation.