String Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of String.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

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

A string is text in code โ€” anything between quotes. Numbers inside quotes are text too: '42' is a string, not a number.

Read the full concept explanation โ†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

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

Common stuck point: '123' is a string, not a number. You can't do math with it until you convert it: int('123') gives the number 123.

Worked Examples

Example 1

easy
Given s = 'Hello World', what is: (a) LENGTH(s), (b) s[0], (c) s[6..10] (substring from index 6 to 10)?

Solution

  1. 1
    Step 1: (a) Count all characters including the space: LENGTH = 11.
  2. 2
    Step 2: (b) s[0] = 'H' (first character, zero-indexed).
  3. 3
    Step 3: (c) s[6..10] = 'World' (characters at indices 6, 7, 8, 9, 10).

Answer

(a) 11, (b) 'H', (c) 'World'.
Strings are sequences of characters accessed by index. Key operations include finding length, accessing individual characters, and extracting substrings.

Example 2

medium
What is the result of concatenating 'Score: ' + STRING(95) + '%'?

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

easy
Given name = 'Alice', what does name + ' is ' + STRING(15) + ' years old' produce?

Example 2

medium
Given word = 'PROGRAMMING', what is the result of each operation? (a) LENGTH(word) (b) word[0] + word[LENGTH(word) - 1] (c) SUBSTRING(word, 3, 4)

Background Knowledge

These ideas may be useful before you work through the harder examples.

assignmentdata types