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

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now