Playing with strings
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, calledsplit()
, 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)
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)
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)
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.
"""
.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
- Write a program to print out the first two verses of The wheels on the bus in Python.
- Can you add to your program to include the other verses where the
wipers go swish, swish, swish
and thepeople go up and down
?
Scratch to Python: Moving from Block- to Text-based Programming

Our purpose is to transform access to education.
We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.
We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.
Learn more about how FutureLearn is transforming access to education