Skip main navigation

How to Manipulate Strings in Python

In this section we will look at some different ways in which you can manipulate strings in Python; hopefully you’ll find this much easier to do compared to using Scratch.

In this section we will look at some different ways in which 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.

 

Here is an example of one such method, called split().

 

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

 

Replace Method

 

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.

 

    1. 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?
This article is from the free online

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

Created by
FutureLearn - Learning For Life

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