What text can do
After this: Use the built-in actions on text and say what each gives back.
On this page · 7 sections · 3 checks
Someone fills in a form and types their email address as ANA@EXAMPLE.COM , with capitals and a space on each side. Before the program can use it, the spaces must go and the letters should be lower case. Someone else sends a line of data such as red,green,blue, and the program needs the three colours one by one.
Jobs like these come up so often that every string carries a set of built-in actions for them. This lesson shows the ones you will use most.
How to use an action
To use an action on a string, write the string, or the name of its box, then a dot, then the action's name, then round brackets. Anything the action needs goes inside the brackets.
s.upper()s.toUpperCase() is the string in capitals. The action gives back a new string; it does not change s. The next lesson is about why.
The actions
Here are the actions, with s holding "Hello World".
| Job | Python | Java | Gives back |
|---|---|---|---|
| capitals | s.upper() | s.toUpperCase() | HELLO WORLD |
| lower case | s.lower() | s.toLowerCase() | hello world |
| swap a piece | s.replace("World", "there") | s.replace("World", "there") | Hello there |
| where is a piece | s.find("World") | s.indexOf("World") | 6 |
| spaces off the ends | s.strip() | s.trim() | the same, with no spaces at either end |
| is a piece in it | "World" in s | s.contains("World") | Truetrue |
Work out the find line by hand: H is at 0, e at 1, l at 2, l at 3, o at 4, the space at 5, and W at 6. So the answer is 6, the position where World starts. When the piece is not in the string at all, findindexOf gives back -1. That number can never be a real position, so it is free to mean "not found".
Watch it happen
Step through the five actions. Keep an eye on the box s: it is the same on the last step as on the first.
s = "Hello World"print(s.upper())print(s.lower())print(s.replace("World", "there"))print(s.find("World"))print(s.find("xyz"))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Change "World" in the find line to "o" and run again. Which o does it find, the first or the second?
Cleaning up
striptrim takes the spaces off both ends and leaves the ones in the middle alone. The trace below starts with eight characters, two spaces, four letters and two spaces, and ends with four.
raw = " milk "clean = raw.strip()print(len(raw))print(len(clean))print("il" in clean)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Taking apart and putting together
split cuts a string into parts at a separator you choose, and gives back the parts as a listas a row of values (an array, see the note below), without the separators. join does the opposite: it takes a list of parts and makes one string, putting a separator between them.
line = "a,b,c"parts = line.split(",")print(len(parts))print(parts[0])print("-".join(parts))3 a a-b-c
String line = "a,b,c";String[] parts = line.split(",");System.out.println(parts.length);System.out.println(parts[0]);System.out.println(String.join("-", parts));3 a a-b-c
- The string has two commas, so cutting at the commas leaves three parts:
a,bandc. The commas themselves are gone. - The parts are a list, so position 0 is the first part,
a. - "-".join(parts)String.join("-", parts) puts a dash between the parts and gives back one string:
a-b-c.
Check yourself
What does this print?
name = "ada"print(name.upper())print(name.find("d"))String name = "ada";System.out.println(name.toUpperCase());System.out.println(name.indexOf("d"));The box s holds "Hello World". What does s.find("xyz")s.indexOf("xyz") give back?
How many parts does "red,green,blue".split(",")"red,green,blue".split(",") give back?
Honesty notes
- Write an action on text: the name of the text, a dot, the action, round brackets.
- Use upper, lower, replace, strip and find (toUpperCase, toLowerCase, replace, trim and indexOf in Java), and say what each gives back.
- Say that find gives back -1 when the piece is not there.
- Split text into parts at a separator (a list in Python, an array in Java), and join parts back into one string.