Article
Joining and slicing strings
A school makes each student a username: the first letter of the first name, then the surname. For Marie Curie that is MCurie. A program builds it by joining strings and cutting pieces out of strings. These are the three things you can do with a string.
Joining strings: concatenation
Concatenation means joining two strings end to end to make one new string.
The AP exam has no symbol for this. Instead, the question tells you in words what a procedure does. In this course we call it concat(a, b), and it means: "return the string a, followed by the string b." Exam questions may use other names, so always read the description.
| Call | Result |
|---|---|
concat("sun", "shine") | "sunshine" |
concat("shine", "sun") | "shinesun" |
concat("Hi", "Sam") | "HiSam" |
Two things to notice. The order matters. And concat adds nothing of its own, so there is no space between Hi and Sam. If you want a space, it must be a string of its own: concat("Hi ", "Sam").
In Python, the + symbol joins strings:
Check your understanding
The procedure concat(a, b) returns the string a followed by the string b. What does concat("fish", "bowl") return?
Length: how many characters
The length of a string is the number of characters in it. Every character counts, including spaces and punctuation. We write length(str) for a procedure that returns it. In Python it is len.
| Call | Result | Why |
|---|---|---|
length("hello") | 5 | h, e, l, l, o |
length("a b") | 3 | a, a space, b |
length("") | 0 | the empty string has no characters |
Substrings: cutting out a piece
A substring is a part of a string made of characters that sit next to each other. The exam describes it like this: substring(str, start, length) returns the characters of str that begin at position start and have the given length. The first character is position 1.
Look at the word ROCKET. Each character has a position:
| Position (exam) | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Character | R | O | C | K | E | T |
| Index in Python | 0 | 1 | 2 | 3 | 4 | 5 |
So substring("ROCKET", 2, 3) starts at position 2 (the O) and takes 3 characters: "OCK". And substring("ROCKET", 6, 1) is just "T".
Python slices a string with square brackets: word[a:b]. Two rules cause most mistakes:
- Python counts from 0, so the first character is at index 0.
- The slice stops before index
b. The character atbis not included.
To turn the exam's call into a Python slice, use word[start - 1 : start - 1 + length]. For substring("ROCKET", 2, 3) that is word[1:4].
The output is OCK, then T, then ROC. Check each against the table. The first slice takes indexes 1, 2 and 3, and stops before 4.
Exam questions keep the start and length inside the string, so you do not need to worry about running past the end.
Check your understanding
The procedure substring(str, start, length) returns length characters of str, beginning at position start. The first character is position 1. What does substring("ROCKET", 3, 2) return?
Worked example: the username
first ← "Marie" last ← "Curie" initial ← substring(first, 1, 1) username ← concat(initial, last) DISPLAY(username) DISPLAY(length(username))
| Line | initial | username |
|---|---|---|
initial ← substring(first, 1, 1) | "M" | |
username ← concat(initial, last) | "M" | "MCurie" |
substring(first, 1, 1) starts at position 1 and takes one character, so it is "M". Joined to "Curie" it gives "MCurie". That has 6 characters: M, C, u, r, i, e. The program displays MCurie 6.
The same program in Python:
Common mistakes
- Counting from 0 in an exam question. In the exam's descriptions the first character is position 1. Python's index 0 is a different system.
- Forgetting that a Python slice stops before the end number.
"ROCKET"[1:4]has 3 characters, not 4. - Using the end position as the length. In
substring(str, 2, 4), the 4 is how many characters to take, not where to stop. - Expecting a space from
concat. Joining"Hi"and"Sam"gives"HiSam". - Not counting spaces in a length.
"sunny day"has 9 characters. - Joining a number to a string in Python.
"Age: " + 12is an error. Write"Age: " + str(12).
On the exam
The exam does not give you string symbols. The question describes each procedure in words, and you trace the code that uses them. Read the description first, then work one step at a time, writing each result down. Typical questions ask what a variable holds after several concat and substring calls, or how many characters a result has.
Key words
Concatenation: joining two strings end to end.
Length: the number of characters in a string, spaces included.
Substring: a part of a string made of characters next to each other.
Position: where a character sits in a string. On the exam the first character is position 1.
Slice: Python's way to take a substring, written word[a:b].