In this lesson you'll be learning to:
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.
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).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 ".Searching within strings is useful for finding specific text or counting occurrences. Python provides methods like count()
, find()
, and rfind()
to help with this.
count(sub)
method returns how many times 'sub' appears in the string. find(sub)
method returns the lowest index where 'sub' is found, or -1 if not present. 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.