Microbit Python Computer Science
Beginner
40 mins
Teacher/Student led
What you need:
Chromebook/Laptop/PC

String Manipulation

In this lesson, you'll explore the essentials of handling text in Python. Learn to understand strings as character sequences, perform operations like concatenation, apply methods to transform text, use slicing for substrings, and practise with hands-on Micro:bit tasks.
Learning Goals Learning Outcomes Teacher Notes

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Introduction

    In this lesson you'll be learning to:

    1. Understand strings: Recognise strings as sequences of characters used for text in Python.
    2. Perform basic operations: Use concatenation to join strings and len() to find their length.
    3. Apply string methods: Transform strings with methods like upper(), lower(), strip(), and replace().
    4. Use slicing: Extract parts of strings using slicing notation for substrings.
    5. Practise with exercises: Complete hands-on tasks to manipulate strings and display results on the Micro:bit.
    6. Apply your skills: Build confidence in handling text data for more complex programs.

    2 - What are Strings?

    Strings are sequences of characters, like text, enclosed in single or double quotes. They are essential for handling any kind of textual data in your programs, such as messages, names, or instructions. In Python, you create a string by assigning text to a variable, for example, message = "Hello, World!". Strings are immutable, meaning you can't change individual characters directly, but you can manipulate them using methods and operations to create new strings.

    Common uses include displaying messages on the Micro:bit, processing user input, or formatting output. Understanding strings is key because text manipulation appears in almost every program, from simple displays to complex data processing.

    3 - Basic String Operations

    Let's start with some basic operations:

    Concatenation joins strings using the + operator, like "Hello" + " " + "World" to make "Hello World".

    The len() function returns the length of a string, counting characters including spaces. These operations are fundamental for building and analysing text.

    In the Micro:bit Python editor, add the following complete code:

    from microbit import *
    
    message = "Hello" + " " + "World"
    display.scroll(message)
    display.scroll(str(len(message)))

    Here's a quick breakdown:

    • from microbit import *: Imports Micro:bit features for display.
    • message = "Hello" + " " + "World": Concatenates to form "Hello World".
    • display.scroll(message): Scrolls the message.
    • display.scroll(str(len(message))): Scrolls the length (11).
    Run your code. You should see "Hello World" scroll, followed by "11".
    Experiment: Change the strings and rerun to see different results.

    4 - String Methods

    String methods are built-in functions in Python that allow you to manipulate strings easily without altering the original string. They are incredibly useful for tasks like formatting text, cleaning up data, or modifying messages for display. Examples include:

    • upper() to convert the entire string to uppercase, which can be handy for standardising text inputs.
    • lower() to convert to lowercase, often used in case-insensitive comparisons.
    • strip() to remove leading and trailing spaces, helping to clean up user-entered data.
    • replace(old, new) to substitute specific text with something else, like updating words in a message.

    Remember, these methods return new strings, so the original remains unchanged unless you reassign it.

    Update your code to this complete version:

    from microbit import *
    
    text = "  Hello World  "
    display.scroll(text.upper())
    display.scroll(text.lower())
    display.scroll(text.strip())
    display.scroll(text.replace("World", "Micro:bit"))

    Here's a quick breakdown:

    • text.upper(): Converts to " HELLO WORLD ".
    • text.lower(): Converts to " hello world ".
    • text.strip(): Removes spaces, resulting in "Hello World".
    • text.replace("World", "Micro:bit"): Changes to " Hello Micro:bit ".
    Run your code. Observe each transformed string scrolling on the Micro:bit display.

    5 - Searching Strings

    Searching within strings is useful for finding specific text or counting occurrences. Python provides methods like count(), find(), and rfind() to help with this.

    • The count(sub) method returns how many times 'sub' appears in the string.
    • The find(sub) method returns the lowest index where 'sub' is found, or -1 if not present.
    • The rfind(sub) method returns the highest index where 'sub' is found, or -1 if not present. 

    These are helpful for tasks like validating input or extracting data from text.

    Update your code to this complete version:

    from microbit import *
    
    text = "Hello, Micro:bit World! Hello again!"
    display.scroll(str(text.count("Hello")))
    display.scroll(str(text.find("c")))
    display.scroll(str(text.rfind("Hello")))
    display.scroll(str(text.find("Python")))

    Here's a quick breakdown:

    • text.count("Hello"): Counts occurrences of "Hello" (2).
    • text.find("Micro:bit"): Finds first index of "c" (9).
    • text.rfind("Hello"): Finds last index of "Hello" (24).
    • text.find("Python"): Returns -1 since not found.
    Run your code. You should see "2", "9", "24", and "-1" scroll on the Micro:bit display.
    Experiment: Change the string or search terms and rerun to see different results.

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more