Skip main navigation
We use cookies to give you a better experience, if that’s ok you can close this message and carry on browsing. For more info read our cookies policy.
We use cookies to give you a better experience. Carry on browsing if you're happy with this, or read our cookies policy for more information.

Skip to 0 minutes and 1 secondYou've already had a look at some of Python's inbuilt functions, such as string and int. You've also had a look at some functions from important modules, such as the choice function from the random module that lets you select random items from various lists. A method is a special type of function to be used on certain types of objects. 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. That was used on a string in the last step and allowed us to split up the string into a list.

Skip to 0 minutes and 46 secondsMethods can act on objects by placing the method name after the object with a full stop between them. There's another method that can be used on strings. This method is called lower and will turn all uppercase characters into lowercase characters.

Skip to 1 minute and 5 secondsSome methods can take arguments. We saw this with the dot split method from the previous section when we wanted to split the string at commas rather than spaces. Within the dot split method, we could pass in the character that we wanted to split the string at. String objects can have numerous methods and it's worth looking at the Python documentation to see all the different methods that can be applied to a string. Let's try a programming task using some string methods to do something interesting. The dot replace method can replace any given substring then other string.

Skip to 1 minute and 44 secondsFor instance, in the sentence, "I wandered lonely as a cloud," I can replace the word, cloud, with the word, clown Here I've saved it as a new variable, called, new one and I can print new one to see the difference between the two sentences.

Skip to 2 minutes and 4 secondsI can use further dot replace methods to replace other words in the sentence. So I can replace the word, wandered and use instead, the word, juggled. I could replace the word, lonely, and change it to, madly, for instance. The end result is going to be a very different sentence to the original one I started with.

Skip to 2 minutes and 33 secondsAnd now a challenge. Let's see if we can use this dot replace method to recreate the nursery rhyme, "The wheels on the bus." There are lots of different ways that this could be accomplished, but let's start with the simplest algorithm.

Skip to 2 minutes and 46 secondsThe original verse will be as follows: Notice how three quotes have been used to make the string that uses multiple lines. Now you could use dot replace to replace all occurrence of "wheels" with "horn," and install this is a new variable. Then you could use dot replace again to replace, "round and round" with "beep beep beep." Then print out the original verse, followed by the verse. Have a go at trying to programme this in Python. You might find it helpful to write a pseudo-code version first. If you managed to create the second verse, then how about other verses, where the wipers go, "swish swish," and the people go, "up and down?"

Skip to 3 minutes and 28 secondsIf you really want to push on new-found Python skills, you can make the replacements happen in a loop to make your code a little more efficient. As always, share your successes and failures in the comments below.

Playing with strings

You’ve already had a look at a few Python’s built-in functions, such as str() and int(). You’ve also had a look at some functions from imported modules, such as choice() from the random module. In this section you’re going to look at some different ways you can manipulate strings in Python.

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. For a full list with explanations, have a look at this page in the Python documentation.

Replacing strings

Let’s try a little programming task and use 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', 'madly')
print(original)
print(new_1)
print(new_2)
print(new_3)

If you run this script, 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 madly as a clown

sony

Challenge

Let’s see if we can use this to recreate the nursery rhyme ‘The wheels on the bus’.

There are lots of different ways this can be accomplished, but lets 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.

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

Have a go at trying to program this in Python. You might find it helpful to write a pseudocode version first. If you manage to create the second verse, then how about trying for the other verses where the wipers go swish, swish, swish and the people go up and down?

If you really want to push your new-found Python skills, can you make the replacements happen in a loop to make your code a little more efficient?

Share your successes and failures with your fellow learners in the comments below!

Share this video:

This video is from the free online course:

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

Raspberry Pi Foundation

Contact FutureLearn for Support