Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Scratch to Python: Moving from Block- to Text-based Programming. Join the course to learn more.

Skip to 0 minutes and 3 seconds In the previous step, you learnt how to manipulate a string using a special type of function called a method. You used the split method to split the string, a, e, i, o, u, into a list by passing a comma as the argument to the split method. Let’s explore some more useful string methods that you can use.

Skip to 0 minutes and 28 seconds You can use the lower method to convert any text into all lowercase. So for example, here is a phrase and it’s all in capitals. And I’m going to create a new variable. And I’m going to use the lower method to convert all of that text into lowercase. If I print it, what we’ll see is when I print, the text is the same, it’s just all in lowercase. You can also use the replace method to replace any given string with any other string. So for example, I’ve created a phrase, I wandered lonely as a cloud. And I can create a new variable, which is based on the original one.

Skip to 1 minute and 14 seconds And I’m going to use the replace method to change the text cloud into clown.

Skip to 1 minute and 25 seconds And I’m going to print that to the screen.

Skip to 1 minute and 29 seconds When we run our program, what we see is our original. And we see the replaced version where cloud has been replaced with clown. And by adding more replace statements to this program and printing them out, I can turn my original phrase, I wandered lonely as a cloud to I juggled wildly as a clown. Now your challenge is to use the replace method to print out the nursery rhyme, The Wheels on the Bus. There are lots of different ways to do this. And the original verse looks like this. You can see how I’ve used three quotes to create a string which spans multiple lines.

Skip to 2 minutes and 14 seconds Now I could use the replace method to swap wheels for horn and round and round for beep, beep, beep. Now can you write a program to print out the first two verses of the wheels on a bus in Python? You could also add more verses, such as where the wipers go swish, swish, swish and the people go up and down. Share your solutions in the comments section. And don’t forget to help each other out if anyone gets stuck. Good luck.

Playing with strings

You’ve already tried out a few of Python’s built-in functions, such as str() and int(). You’ve also had a look at some functions from imported modules, for example choice() from the random module. In this section you’re going to look at some different ways you can manipulate strings in Python; hopefully you’ll find this much easier to do compared to using Scratch.

Methods

A method is a special type of function that can be used on certain types of object. In Python, everything is an object, so there are lots of different methods that can be used.

You’ve already briefly seen one such method, called split(), when you used it on a string in the previous step.

vowels = "a e i o u"
list_of_vowels = vowels.split()
print(list_of_vowels)

To make a method act on an object, place the method name after the object, with a full stop in between.

Changing case

Here’s another method that can be used on strings — try it out to see what it does.

angry = 'THIS IS SOME TEXT'
calm = angry.lower()
print(calm)

Splitting strings

Some methods can take arguments inside the brackets. .split() is an example of one of these methods. Have a go at running this code:

vowels = "a,e,i,o,u"
list_of_vowels = vowels.split(',')
print(list_of_vowels)

There are lots of string methods to play around with, all built into the Python language — unlike in Scratch where you would have to create your own blocks or import an extension. For a full list with explanations, have a look at this page in the Python documentation.

Replacing strings

This is a little programming task that uses some string methods to do something interesting.

The .replace() method can replace any given substring with another string. For example:

original = 'I wandered lonely as a cloud'
new_1 = original.replace('cloud', 'clown')
new_2 = new_1.replace('wandered', 'juggled')
new_3 = new_2.replace('lonely', 'wildly')
print(original)
print(new_1)
print(new_2)
print(new_3)

If you run this program, you get the following output:

I wandered lonely as a cloud
I wandered lonely as a clown
I juggled lonely as a clown
I juggled wildly as a clown

The wheels on the bus

I am going ask you to use the .replace() method to print out the nursery rhyme The wheels on the bus.

There are lots of different ways this can be accomplished, but I am going to start with the simplest algorithm. The original verse will be as follows:

verse = """
The wheels on the bus go round and round,
Round and round, round and round,
The wheels on the bus go round and round,
All day long.
"""

Notice how three quotes have been used to make a string that uses multiple lines.

I can use .replace() to replace all occurrences of wheels with horn, and store this as a new variable. Then I can use .replace() again to replace round and round with beep, beep, beep. Then print out the original verse, followed by the new verse.

Task

  1. Write a program to print out the first two verses of The wheels on the bus in Python.
  2. Can you add to your program to include the other verses where the wipers go swish, swish, swish and the people go up and down?

Share your solutions in the comments section by using a link from Pastebin. Remember to support each other. If anyone is asking for help, you don’t have to wait for a course facilitator to jump in!

Share this video:

This video is from the free online course:

Scratch to Python: Moving from Block- to Text-based Programming

Raspberry Pi Foundation