4. Computational Thinking, Problem-Solving and Programming

Strings

Strings in Computational Thinking and Programming

Introduction

students, imagine sending a text message, storing a student ID, or checking whether a website username contains a space. All of these tasks involve strings. In computer science, a string is a sequence of characters used to represent text. Strings are one of the most common data types in programming because computers need to read, store, compare, display, and transform human-readable information all the time 📱💻

In this lesson, you will learn the main ideas and terminology behind strings, how strings are used in problem-solving, and why they matter in IB Computer Science HL. By the end, you should be able to explain what strings are, use common string operations, and connect string handling to algorithmic thinking, abstraction, and solution design.

Learning objectives

  • Explain the main ideas and terminology behind strings.
  • Apply IB Computer Science HL reasoning and procedures related to strings.
  • Connect strings to computational thinking, problem-solving, and programming.
  • Summarize how strings fit into solution design and evaluation.
  • Use examples related to strings in realistic programming situations.

What is a string?

A string is an ordered sequence of characters. A character can be a letter, number, symbol, space, or punctuation mark. For example, $"Hello"$, $"IB Computer Science"$, $"2026"$, and $"A7!"$ are all strings.

Strings are different from numbers because they are treated as text. The string $"123"$ is not the same as the integer $123$. This matters when a program needs to decide whether to calculate with a value or display it as text. For example, a phone number like $"0123456789"$ is usually stored as a string, because the leading zero is important and no arithmetic is needed.

In many languages, strings are written inside quotation marks. Some languages use double quotes, such as $"name"$, while others may allow single quotes for single characters. A single character like $'A'$ is often stored as a character type, not a full string, depending on the language.

Key terminology

  • Character: a single symbol such as $"A"$, $"7"$, or $"!"$.
  • String length: the number of characters in a string.
  • Index: the position of a character in a string.
  • Substring: a smaller string taken from inside a longer string.
  • Concatenation: joining two or more strings together.

Most programming languages count positions starting at $0$. That means the first character in a string is at index $0$, the second at index $1$, and so on. This is important because off-by-one errors are very common when working with strings.

For example, in $"computer"$:

  • index $0$ is $"c"$
  • index $1$ is $"o"$
  • index $2$ is $"m"$
  • index $7$ is $"r"$

If a string has length $n$, its last character is usually at index $n-1$.

Common string operations

Programming often involves performing operations on strings to solve a problem. These operations are simple on the surface but very useful in real systems such as login forms, search engines, and chat apps.

1. Concatenation

Concatenation joins strings together. For example:

$$"Hello" + " " + "students" = "Hello students"$$

This is useful for creating messages, formatting output, or building file paths. Suppose a program stores a first name and last name separately. It can combine them into a full name for display.

Example:

  • $firstName = "Amina"$
  • $lastName = "Khan"$
  • fullName = firstName + " " + lastName

The result is $"Amina Khan"$.

2. Length

The length of a string tells us how many characters it contains. If a password rule requires at least $8$ characters, the program checks the string length.

Example:

  • $"cat"$ has length $3$
  • $"Hello!"$ has length $6$

This helps with validation. A form can reject a username that is too short or a message that exceeds a limit.

3. Substrings

A substring is a piece of a larger string. For example, $"Comp"$ is a substring of $"Computer"$.

Substring extraction is useful when reading part of a code, an ID, or a date. For instance, a student number like $"IB2026-041"$ might be split so the program can read the year portion and the sequence number separately.

4. Search and match

Programs often need to check whether a string contains certain characters or words. This is called searching or matching.

For example, a school email address may need to contain $"@"$. A program might also check whether a message contains banned words or whether a filename ends with $".txt"$.

5. Comparison

Strings can be compared to see if they are equal or, in some languages, whether one comes before another alphabetically.

For example:

  • $"apple" = "apple"$ is true
  • $"Apple" = "apple"$ may be false because capital letters matter

This shows that string comparison can be case-sensitive. In many applications, programs convert text to the same case before comparing it, such as making everything lowercase.

Strings in computational thinking

Strings are a good example of abstraction. A computer stores text as data, but people think of it as names, messages, or labels. Abstraction lets programmers focus on what the string means in the problem rather than the low-level binary representation.

Strings also support decomposition. A complicated task can be broken into smaller string-handling steps. For example, validating an email address may involve:

  1. checking that it contains $"@"$
  2. checking that it contains a domain name
  3. checking that it does not contain spaces
  4. checking that it follows a general format

Each step is simpler than handling the whole problem at once.

Strings also connect to algorithmic thinking because many solutions follow a clear sequence of steps. For instance, a program that formats a name might:

  1. read the input string
  2. remove extra spaces
  3. convert the first letter to uppercase
  4. combine the parts into a final result

A clear algorithm helps programmers avoid mistakes and makes the solution easier to test.

Strings in programming and data processing

Strings are used in nearly every type of program. In data processing, text must often be cleaned before it can be analyzed. This is especially important when data comes from users, sensors with labels, websites, or files.

Real-world examples

  • Usernames and passwords: checking length, allowed characters, and format 🔒
  • Search bars: finding matching words in text
  • Chat apps: sending and displaying messages
  • Spreadsheets: handling names, addresses, and codes
  • File systems: checking file extensions such as $".jpg"$ or $".pdf"$
  • ID systems: storing school IDs, passport numbers, or product codes

Suppose a program stores a country code and a number separately:

  • $country = "MY"$
  • $number = "2047"$

It can create a full code using concatenation:

$$"MY" + "-" + "2047" = "MY-2047"$$

This is a simple example of solution design: the program organizes data so it is useful for display and processing.

String processing tasks

Some common tasks include:

  • converting uppercase to lowercase
  • trimming spaces at the beginning or end
  • finding a specific character
  • replacing one word with another
  • splitting a string into parts

These tasks are useful for cleaning input. For example, if a user enters $" students "$, the program may remove extra spaces before saving the value. This prevents problems when comparing text later.

Evaluating string-based solutions

In IB Computer Science HL, it is important not only to write a solution but also to evaluate it. When working with strings, think about whether the program is accurate, efficient, and easy to use.

Questions to ask when evaluating

  • Does the program handle empty strings?
  • What happens if the input contains spaces?
  • Is comparison case-sensitive?
  • Can the program process very long strings efficiently?
  • Does the solution produce the correct output for all valid inputs?

For example, consider a program that checks whether a username is valid. If it only checks length but not forbidden symbols, it may accept invalid input. If it removes spaces automatically, it may improve usability. If it rejects names with hyphens or apostrophes, it may be too strict. Good evaluation means testing realistic examples, not only ideal ones.

Common errors

  • Off-by-one errors when accessing characters by index
  • Case mismatch when comparing text
  • Extra spaces causing incorrect results
  • Confusing numbers and strings such as treating $"007"$ as the number $7$
  • Assuming every string has at least one character

Testing with different examples helps reveal these problems. Good programmers often use test data such as normal, boundary, and invalid inputs.

Conclusion

students, strings are a basic but powerful part of programming. They represent text, support communication between humans and computers, and appear in many real-world applications. Understanding strings means knowing terms like character, length, substring, and concatenation, as well as being able to use them in algorithms and data-processing tasks.

In Computational Thinking, strings help programmers abstract real information into data, break problems into smaller parts, and design solutions that are clear and testable. In IB Computer Science HL, string handling is not just a technical skill; it is a foundation for building useful programs that can read, validate, transform, and present information correctly ✅

Study Notes

  • A string is a sequence of characters used to represent text.
  • A character is a single symbol such as $"A"$, $"7"$, or $"!"$.
  • Strings are usually written inside quotation marks.
  • Strings are different from numbers, even if they contain digits, such as $"123"$.
  • The length of a string is the number of characters it contains.
  • Many programming languages use zero-based indexing, so the first character is at index $0$.
  • A substring is part of a longer string.
  • Concatenation joins strings together.
  • String comparison may be case-sensitive, so $"Apple"$ and $"apple"$ may not match.
  • Strings support abstraction by representing human text as computer data.
  • Strings support decomposition by allowing problems to be broken into smaller tasks.
  • Common tasks include searching, matching, trimming, splitting, and replacing text.
  • String handling is important in validation, data cleaning, search, messaging, and file processing.
  • Good evaluation includes checking empty strings, spaces, case issues, and boundary cases.
  • String errors often come from off-by-one mistakes, confusion between text and numbers, and unhandled input formats.

Practice Quiz

5 questions to test your understanding