Skip to main content

Unit 3 · Topic 3.4

3.4 Strings

Strings are text: sequences of characters. This topic covers joining strings together (concatenation) and pulling out pieces of them (substrings), and how to work with string procedures that a question defines for you.

Key terms

  • string
  • character
  • concatenation
  • substring

Strings

A string is an ordered sequence of characters, written in quotes: "hello", "Room 214", "!?". Letters, digits, spaces and symbols are all characters. The string "" with nothing in it is the empty string.

A string of digits isn't a number. "42" is two characters, a 4 and a 2. You can't do arithmetic with it the way you can with 42.

Comparing strings with = checks for an exact match, character by character. "Cat" = "cat" is false, because an uppercase C and a lowercase c are different characters, and "cat " with a trailing space doesn't equal "cat" either. This is one reason data often needs cleaning (2.3) before a program can match it.

The length of a string is the number of characters in it, counting spaces and punctuation. "Hi, Jo" has 6 characters.

Concatenation

Concatenation glues strings together, one right after another, into a single new string. Joining "sun" and "flower" gives "sunflower".

Concatenation adds nothing in between. Joining "Hi" and "there" gives "Hithere". If you want a space, you have to include it, either in one of the strings ("Hi ") or as its own string (" ").

The original strings don't change. Concatenation builds a new string.

Substrings

A substring is part of an existing string: a run of characters next to each other. "flow", "sun" and "lowe" are all substrings of "sunflower". "sfl" is not, because those letters aren't next to each other.

Positions in a string are counted from 1, just like list indexes. In "sunflower": s is 1, u is 2, n is 3, f is 4, l is 5, o is 6, w is 7, e is 8, r is 9.

String procedures on the exam

The exam reference sheet doesn't include any string procedures. When a question needs one, it tells you exactly what it does. For example, a question might say:

  • concat(str1, str2) returns a new string made of str1 followed by str2.
  • substring(str, start, length) returns the part of str that begins at position start and has length characters.
  • len(str) returns the number of characters in str.

Read the definition every time

These names and details are only examples. Read each question's definition carefully, because the details (such as whether the second number is a length or an ending position) change from question to question.

A good habit: write the string out with position numbers under each character before you work out any substring. It takes ten seconds and prevents most off-by-one mistakes.

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Evaluating a substring

    The procedure substring(str, start, length) returns the part of str beginning at position start with length characters. Positions start at 1. What does this display?word ← "sunflower" part ← substring(word, 4, 4) DISPLAY(part)

    Show the solution
    1. Step 1: Number the characters: s=1, u=2, n=3, f=4, l=5, o=6, w=7, e=8, r=9.
    2. Step 2: Start at position 4 (f) and take 4 characters: f, l, o, w.

    Answer: flow

  2. Example 2

    Building a username

    Using the same substring procedure and concat(str1, str2), which returns str1 followed by str2, what does this display?first ← "Maya" last ← "Okafor" username ← concat(substring(first, 1, 1), last) username ← concat(username, "27") DISPLAY(username)

    Show the solution
    1. Step 1: substring(first, 1, 1) takes 1 character starting at position 1 of "Maya": "M".
    2. Step 2: concat("M", "Okafor") gives "MOkafor".
    3. Step 3: concat("MOkafor", "27") gives "MOkafor27". The "27" is a string here, so it's joined on, not added.

    Answer: MOkafor27

  3. Example 3

    The missing space

    What does this display?greeting ← concat("Hi", "there") DISPLAY(greeting)

    Show the solution
    1. Step 1: concat puts the second string right after the first with nothing in between.
    2. Step 2: "Hi" followed by "there" is "Hithere".

    Answer: Hithere (with no space between the words)

Common mistakes

  • Expecting concatenation to add a space. It joins strings exactly as they are.
  • Counting string positions from 0. On the exam, the first character is at position 1.
  • Treating a substring procedure's second number as an ending position when the question defines it as a length, or the other way around. Reread the definition.

On the exam

  • String questions always define the procedures they use. Expect to evaluate a short expression or pick the code that produces a given string.
  • Write position numbers under the characters before you count. Off-by-one answers are common distractors.

Connected topics

Videos

Check yourself

3 questions on 3.4 Strings. Pick an answer to see if you got it, and why.

Question 1 of 3

The procedure concat(str1, str2) returns a single string made of str1 followed by str2. The procedure substring(str, start, length) returns the part of str that begins at index start and is length characters long. The first character of a string is at index 1. What is displayed when the following code segment is run? word ← "computer" part ← substring(word, 4, 3) DISPLAY(concat(part, "s"))

Question 2 of 3

The procedure concat(str1, str2) returns a single string made of str1 followed by str2. The procedure substring(str, start, length) returns the part of str that begins at index start and is length characters long. The first character of a string is at index 1. The variable first holds "Maya" and the variable last holds "Ortiz". Which expression evaluates to "Ortiz, M"?

Question 3 of 3

Which of the following strings is a substring of "notebook"?

0 of 3 answered