Unit 3: Programming · Strings

Article

Text in variables

5 min read

Your name, a text message and a password are not numbers. A program still has to store them. It uses a kind of value called a string.

A string is text in quotes

A string is a row of characters. A character is one letter, digit, symbol or space. In a program, you put quote marks around a string. The quotes tell the computer: "this is text, not a command and not a variable name."

On the exam, programs are written in AP pseudocode. In this course you can also run the same programs in Python. Here is one program in both:

AP pseudocode
name ← "Mia"
greeting ← "Hello"
DISPLAY(greeting)
DISPLAY(name)
Runs in your browser

The quote marks are not part of the string, so they are not shown. The program displays Hello and then Mia.

There is one small difference. On the exam, DISPLAY shows its value followed by a space, so the two lines above appear together as Hello Mia. Python's print starts a new line each time.

Change "Mia" to your own name and run it again.

Check your understanding

What is displayed when this program runs?

AP pseudocode
city ← "Lima"
DISPLAY(city)

Quotes make the difference

Compare name and "name". Without quotes, name is a variable: the computer looks up what it holds. With quotes, "name" is the text n-a-m-e.

AP pseudocode
word ← "cat"
DISPLAY(word)
DISPLAY("word")

The first DISPLAY shows what the variable word holds, which is cat. The second shows the text word. Together they appear as cat word.

Quotes also keep spaces and capital letters. A space is a character, so "a b" has three characters. And "Cat" is a different string from "cat". A string with no characters at all is the empty string, written "".

A number is not the same as a string

5 is a number. "5" is a string that contains the character 5. They look alike when displayed, but they are different data types. A data type is the kind of value something is: so far, a number or a string.

ValueData typeCan you do arithmetic with it?
5numberYes: 5 + 3 is 8
"5"stringNo: it is text, not an amount
"hello"stringNo
2.5numberYes

Python can tell you the type of a value. Run this and read the answers:

Runs in your browser

int is Python's name for a whole number and str is its name for a string. int("5") turns the string into the number 5, and str(5) turns the number into the string "5".

When should you use a string for digits? Use one when you will never do arithmetic with the value. A phone number or a student ID such as 00742 should be a string. If it were stored as a number, the zeros at the front would disappear and it would become 742.

Check your understanding

A program runs the line x ← "8". Which statement is true?

Worked example

Trace this program. The last column says what each variable holds after the line.

AP pseudocode
first ← "Ada"
number ← 3
text ← "3"
copy ← first
first ← "Bo"
DISPLAY(copy)
DISPLAY(number + 1)
DISPLAY(text)
Linefirstnumbertextcopy
first ← "Ada""Ada"
number ← 3"Ada"3
text ← "3""Ada"3"3"
copy ← first"Ada"3"3""Ada"
first ← "Bo""Bo"3"3""Ada"

Now the three DISPLAY lines. copy holds Ada, so Ada is shown. number + 1 is 3 + 1, so 4 is shown. text holds the string "3", so 3 is shown. The program displays Ada 4 3.

Notice that the 3 at the end looks like a number. Displayed values do not show their quotes, so you cannot tell the type from the output. You must look at how the variable was assigned.

Common mistakes

  • Forgetting the quotes. name ← Mia does not store the text Mia. The computer looks for a variable called Mia, which has no value.
  • Thinking "5" is a number. If it is inside quotes, it is a string, even when it contains only digits.
  • Thinking the quotes are part of the text. They only mark the start and end. DISPLAY does not show them.
  • Ignoring spaces and capitals. A space is a character. "Hi" and "hi" are different strings.
  • Mixing a number and a string in Python. "5" + 1 is an error in Python. Convert first with int or str.

On the exam

You will meet strings in code you must trace: you work out what a variable holds and what DISPLAY shows. Watch for questions that give a number and a string that look alike, or that copy one variable into another. The exam also asks which values should be stored as strings, such as IDs and phone numbers.

Key words

String: a data type for text, written inside quote marks.

Character: one letter, digit, symbol or space in a string.

Data type: the kind of value a variable holds, such as a number or a string.

Empty string: a string with no characters, written "".

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.