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. Strings support operations like concatenation (joining), slicing (extracting substrings), searching, and comparison, and can contain letters, digits, spaces, and special characters.
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.
Sense of Study hint: When working with strings, remember that the + operator concatenates (joins) strings rather than adding them numerically. To combine text with numbers, convert the number to a string first. Use indexing to access individual characters, remembering that indices start at 0.
Worked Examples
Example 1
easyAnswer
First step
Full solution
- 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).
Example 2
mediumExample 3
mediumExample 4
hardExample 5
challengePractice Problems
Try these problems on your own first, then open the solution to compare your method.
Example 1
easyExample 2
mediumExample 3
easyExample 4
easyExample 5
easyExample 6
easyExample 7
easyExample 8
easyExample 9
easyExample 10
easyExample 11
mediumExample 12
mediumExample 13
mediumExample 14
mediumExample 15
mediumExample 16
mediumExample 17
mediumExample 18
mediumExample 19
mediumExample 20
challengeExample 21
challengeExample 22
challengeExample 23
easyExample 24
easyExample 25
easyExample 26
easyExample 27
easyExample 28
easyExample 29
mediumExample 30
mediumExample 31
mediumExample 32
mediumExample 33
mediumExample 34
mediumExample 35
mediumExample 36
mediumExample 37
mediumExample 38
mediumExample 39
mediumExample 40
hardExample 41
hardExample 42
hardExample 43
challengeRelated Concepts
Background Knowledge
These ideas may be useful before you work through the harder examples.