Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Programming Fundamentals · Grade 6-8

String

A data type that represents a sequence of characters (text), enclosed in quotation marks.


Quick Answer

A data type that represents a sequence of characters (text), enclosed in quotation marks. Use a string for anything a program stores as text: a username, a chat message, a password, a file name. Each character sits at a numbered position starting from 0, so if name = 'Maria', then name[0] is 'M'. You can join strings with +, cut out pieces, search inside them and compare them, but you cannot do arithmetic with them: '3' + '4' gives '34', not 7.

Why This Matters

Almost everything a person types into a program arrives as a string, even a number typed into a box. Knowing that lets you convert text to a number before adding, check that an email address contains '@', or build a greeting like 'Welcome back, Maria'. Without it, a shopping basket that should add 1 to a quantity of '2' shows '21' instead of 3.

Intuitive Explanation

Picture a string as a row of numbered tiles, one character per tile, like the letter tiles in a word game. The string 'cat' is three tiles: c at position 0, a at 1, t at 2. Spaces, digits and punctuation take tiles too, so 'New York' has 8 tiles, and len('New York') is 8. The last tile is always at position len - 1, here 7.

Operations work on the tiles. + lays two rows end to end: 'Hello, ' + 'Maria' gives 'Hello, Maria'. A slice copies a stretch of tiles: 'Maria'[0:3] is 'Mar', starting at position 0 and stopping before 3. A search asks whether a shorter row appears inside: '@' in 'jo@mail.com' is True. In Python and Java a string cannot be edited tile by tile, so fixing a lowercase name means building a new string: 'M' + name[1:] turns 'maria' into 'Maria'.

The trap is digits. '42' is a string of two characters, the tile 4 and the tile 2, not the number forty-two. So '42' + '8' gives '428', and '42' + 8 is an error in Python. When text holds a quantity you want to calculate with, convert it first: int('42') + 8 is 50. When digits are a label, like a locker code 007, keep them as a string so the leading zeros survive.

Think of it like this. Beads on a necklace, one letter per bead and numbered from 0, behave like a string: you can count the beads, read any one by its number, clip two necklaces together or copy a stretch. The picture fails when you try to swap a single bead; in Python and Java you must thread a whole new necklace instead.

Core idea. Strings are sequences — you can access individual characters by index, search for substrings, and combine strings.


When to Use

Use a string whenever the value is text or a label made of characters: names, messages, passwords, file names, a phone number with a leading 0. Signals are quotation marks in the code and tasks such as joining words, counting letters, taking an initial, or checking whether some text contains a symbol. If you need to calculate with the value, convert it to an integer or float first. If the value can only be yes or no, store a Boolean rather than the text 'yes'.

Tip. Ask: Is the value text in quotation marks, even if it is made of digits, so + joins it instead of adding?

How to Recognize It

Check how the value is written and what the program does with it: reads it, joins it, cuts it apart or calculates with it.

  1. Is the value written inside quotation marks, such as 'Maria' or '2024'?

    Yes means it is a string, even when every character is a digit. A bare 2024 with no quotes is an Integer you can do arithmetic with.

  2. Does the program join pieces of text, such as 'Hi, ' + name?

    Yes means string concatenation. If one piece is a number, turn it into text first with str(), or Python raises a TypeError.

  3. Does it ask for a character or a piece at a numbered position, such as word[0] or word[1:3]?

    Yes means string indexing from 0: in 'pizza', word[0] is 'p' and word[1:3] is 'iz'. If each position holds a separate value, such as a score, it is an Array instead.

  4. Must the value be added, averaged or compared by size as a number?

    Yes means convert it first, as int('12') gives 12. Strings compare letter by letter, so '9' > '10' is True, which is wrong if you meant the numbers.

String vs Integer vs Array vs Variable

Digits, square brackets and names appear in all four, so text, numbers, lists and variable names blur together. Ask what the value is made of and whether it sits inside quotation marks.

String

Meaning
A string is a sequence of characters in quotation marks, treated as text even when the characters are digits.
Key test
Quotes around it; + joins, and each position holds one character.
Example
'7' + '7' gives '77', and len('hello') gives 5.

Integer

Meaning
An integer is a whole number written without quotes, so arithmetic works on it.
Key test
No quotes; + adds, so 7 + 7 gives 14.
Example
age = 13, then age + 1 gives 14; with age = '13' the same line is an error in Python.

Array

Meaning
An array is an ordered list of separate values, such as numbers or names, each at a numbered position.
Key test
Each position holds a whole value, not one character.
Example
names = ['Ana', 'Ben'] holds two strings; names[1] is 'Ben', and names[1][0] is 'B'.

Variable

Meaning
A variable is a name that refers to a stored value, and that name is written without quotation marks.
Key test
No quotes: name means the stored value, while 'name' means four letters.
Example
If name = 'Zoe', print(name) shows Zoe, but print('name') shows the word name.

Formula & Notation

Worked Examples

Example 1 — Build a username

Easy

A school builds usernames from a first name and a birth year. With first = 'Maria' and year = '2012', the program runs user = first[0:3] + year[2:4]. What is user, and how many characters does it have?
Show the steps and the answer
  1. first[0:3]: take positions 0, 1 and 2 of 'Maria', which are M, a and r, giving 'Mar'.

    A slice starts at the first number and stops just before the second.

  2. year[2:4]: in '2012', position 2 is '1' and position 3 is '2', giving '12'.

    The year is stored as a string, so its digits are characters that can be sliced like letters.

  3. Join them: 'Mar' + '12' = 'Mar12'.

    + between two strings places them end to end; nothing is added as numbers.

  4. Count: len('Mar12') = 5.

    Every letter and digit takes one position: 3 + 2 = 5.

Answer: user = 'Mar12', which has 5 characters.

Takeaway: Slice positions are counted from 0 and stop before the second number; + glues text together.

Example 2 — Digits that should be a number

Standard

A form asks for a user's age, and the program receives age = '15'. It then runs next_year = age + '1'. What is next_year, and what should the code be?
Show the steps and the answer
  1. Check the type: age holds '15' in quotes, a string made of the characters 1 and 5.

    Anything typed into a form arrives as text, even when it looks like a number.

  2. Evaluate age + '1': joining gives '151'.

    With two strings, + concatenates, so the 1 is attached to the end.

  3. Convert first: next_year = int(age) + 1 = 15 + 1 = 16.

    Age is a quantity to calculate with, so it must become an integer before the arithmetic.

  4. To show it in a sentence, convert back: 'Next year you will be ' + str(next_year) gives 'Next year you will be 16'.

    Joining text with a number needs the number turned into a string first.

Answer: age + '1' gives '151'. The code should be next_year = int(age) + 1, which gives 16.

Takeaway: Quotation marks decide the type: digits inside them are text. Convert with int() before arithmetic and with str() before joining into a sentence.

Example 3 — Find the mistake: counting from 1

Application

Jo wants the first and last letters of word = 'robot' and writes first = word[1] and last = word[5]. Find the mistake and correct it.
Show the steps and the answer
  1. Number the characters of 'robot': r = 0, o = 1, b = 2, o = 3, t = 4.

    Positions start at 0, so a 5-letter word uses positions 0 to 4.

  2. word[1] is 'o', the second letter, not the first.

    Jo counted from 1, the way people usually count, which shifts every position by one.

  3. word[5] does not exist, so Python stops with an IndexError.

    The last position is len(word) - 1 = 5 - 1 = 4.

  4. Correct it: first = word[0] gives 'r', and last = word[len(word) - 1] gives 't'.

    Using len(word) - 1 finds the last letter of a word of any length, not only five letters.

Answer: first = word[0] is 'r' and last = word[4] (or word[len(word) - 1]) is 't'.

Takeaway: The first character is at position 0 and the last at len - 1; counting from 1 puts every answer one place too far.

Where You'll See It

  • Texting a friend

    When you type a message, your phone stores every letter, emoji, and space as a string. Adding 'lol' to the end of your message is string concatenation.

  • Your username on a game

    A username like 'Player_2008' is a string even though it contains digits — the digits are characters, not a number you can add to, which is why '2008' in a name can't be used in math.

  • Reading the first letter of a name

    Sorting a class roster by first letter means looking at character index 0 of each name string, e.g. 'Maria'[0] is 'M'.

Common Mistakes

  1. The slip: Numbers inside quotes are still numbers, so '5' + '3' equals 8.

    Why it's tempting: It looks like arithmetic because both sides are digits, and in math 5 + 3 is 8.

    Instead: Quoted digits are a string, and + on strings concatenates: '5' + '3' is '53'. You must convert with int('5') first to get 8.

  2. The slip: The first character of a string is at index 1.

    Why it's tempting: Humans count starting at 1 ('the first character'), so index 1 feels like the start.

    Instead: String indexing starts at 0, so the first character is s[0] and the last is s[len(s)-1].

  3. The slip: You can change a letter in place, like s[0] = 'H', because a string is just a list of letters.

    Why it's tempting: Indexing to read a character works, so writing to that same index seems like it should too.

    Instead: In Python and Java strings are immutable: s[0] = 'H' is an error. You build a new string instead, e.g. 'H' + s[1:].


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. s = 'COMPUTER'. What are s[0], s[3] and len(s)?

    Hint: Indexes start at 0, so C is at position 0.

  2. first = 'Ada' and last = 'Lovelace'. What is full = first + ' ' + last, and how long is it?

    Hint: + joins strings. Count the space as a character.

  3. word = 'butterfly'. What are word[0:6] and word[6:]?

    Hint: A slice [a:b] starts at index a and stops just before index b.

  4. What does '3' + '4' give in Python? What does 3 + 4 give? Why are they different?

    Hint: Look at the quotation marks. What type is each value?

  5. Should a phone number like 0412 555 019 be stored as a string or as an integer?

    Hint: Would you ever do arithmetic on it? What happens to the leading 0?

  6. A student writes print('Score: ' + 25) in Python and expects Score: 25. What actually happens, and how should the line be fixed?

    Hint: Can + join a string and an integer directly?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

How does a computer store a letter?

Each character is stored as a number from a code table. In Unicode, which almost every computer now uses, 'A' is 65, 'a' is 97 and the digit '0' is 48, and Python shows these codes with ord('A'). That is why sorting strings puts capital letters before lowercase ones: 'Zebra' comes before 'apple', because 90 is smaller than 97. Emoji have codes too, far higher up the table.

Is an empty string the same as nothing?

No. '' is a real string with length 0: you can join to it, print it, or check that len('') is 0. Programs often start with message = '' and add pieces in a loop. A single space ' ' is different again: it looks empty but has length 1, which is why forms that reject blank names usually strip spaces first. In an if statement, Python treats '' as false and every other string, even ' ', as true.

Why does len('hello world') give 11?

Every character counts, including the space between the two words, so 5 + 1 + 5 = 11. Punctuation, spaces and digits are all characters. The last valid position is one less than the length, so 'hello world'[10] is 'd' and 'hello world'[11] is an error. In JavaScript, a single emoji such as a smiley face often has length 2, so a text box with a character limit can cut one in half.

Learning Path

String

You are here

Next →

Boolean
You need Assignment (storing values in variables) and Data Types (every value has a kind that decides its operations) first. String adds text: characters at numbered positions from 0, joining with +, slicing and searching. Next, Boolean covers the true/false values that string tests such as '@' in email or name == 'Maria' produce, which is how programs decide what to do with text.

See Also