String Examples: 48 Problems with Answers
Worked examples of String to try before you open each solution, then practice problems with answers.
Worked Examples
Example 1
easyShow solution
Answer
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 each one, then open the solution to compare your method.
Example 1
easyExample 2
medium(a) LENGTH(word)
(b) word[0] + word[LENGTH(word) - 1]
(c) SUBSTRING(word, 3, 4)
Example 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
challengeWhat 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.
Ask yourself: Is the value text in quotation marks, even if it is made of digits, so + joins it instead of adding?
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.
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.
Read the full lesson on StringRelated concepts
Background knowledge
These ideas may be useful before you work through the harder examples.