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
easySolution
- 1 Step 1: (a) Count all characters including the space: LENGTH = 11.
- 2 Step 2: (b) s[0] = 'H' (first character, zero-indexed).
- 3 Step 3: (c) s[6..10] = 'World' (characters at indices 6, 7, 8, 9, 10).
Answer
Example 2
mediumPractice Problems
Try these problems on your own first, then open the solution to compare your method.
Example 1
easyExample 2
mediumRelated Concepts
Background Knowledge
These ideas may be useful before you work through the harder examples.