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

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

Answer

(a) 11, (b) 'H', (c) 'World'.

First step

1
Step 1: (a) Count all characters including the space: LENGTH = 11.

Full solution

  1. 2
    Step 2: (b) s[0] = 'H' (first character, zero-indexed).
  2. 3
    Step 3: (c) s[6..10] = 'World' (characters at indices 6, 7, 8, 9, 10).
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) + '%'?

Example 3

medium
Build a greeting: `name = "Sam"`; produce `"Hello, Sam!"` using concatenation.

Example 4

hard
Write a one-liner check: is `"level"` a palindrome?

Example 5

challenge
Why is building a string by repeated `+=` in a loop O(n2)O(n^2) in Python, and what is the idiomatic fix?

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)

Example 3

easy
Given `s = "hello"`, what is `s[0]`?

Example 4

easy
What is the length of `"world"`?

Example 5

easy
Concatenate `"foo" + "bar"`. What is the result?

Example 6

easy
Given `s = "cat"`, what is `s[2]`?

Example 7

easy
Given `s = "abcdef"`, what is the slice `s[1:3]`?

Example 8

easy
Given `s = "data"`, what is `s[-1]` (last character)?

Example 9

easy
How many characters are in `"a b"` (with the space)?

Example 10

easy
Repeat a string: what is `"ab" * 3`?

Example 11

medium
Given `s = "program"`, what is `s[0:3] + s[3:]`?

Example 12

medium
Given `s = "abcdef"`, what is `s[::2]` (every other character)?

Example 13

medium
Reverse `"abc"` using a slice. What is `"abc"[::-1]`?

Example 14

medium
Given `s = "banana"`, how many times does the character `'a'` appear?

Example 15

medium
Given `first = "Ada"`, `last = "Lovelace"`, build `first + " " + last`. What is the result?

Example 16

medium
In a strongly-typed language, `"Age: " + 30` errors. What is the fix to get `"Age: 30"`?

Example 17

medium
Strings are immutable in Python. After `s = "cat"`, does `s = s + "s"` change the original string object?

Example 18

medium
Given `s = "hello"`, what is the index of the last character using `len`?

Example 19

medium
Given `s = "mississippi"`, what is `s[4:7]`?

Example 20

challenge
Given `s = "abcdefg"`, what does `s[1:6:2]` produce?

Example 21

challenge
Check if `"racecar"` is a palindrome by comparing it to its reverse. Result?

Example 22

challenge
Build the substring of `"computer"` from index 2 up to but not including the last character. What is it?

Example 23

easy
Given `s = "python"`, what is `s[1]`?

Example 24

easy
What is the length of `"science"`?

Example 25

easy
Concatenate `"open" + "AI"`.

Example 26

easy
Given `s = "sun"`, what is `s[-1]`?

Example 27

easy
What does `"go" * 2` produce?

Example 28

easy
Given `s = "code"`, what is `s[0:2]`?

Example 29

medium
Given `s = "abcde"`, what is `s[1:4]`?

Example 30

medium
Given `s = "banana"`, what is `s.upper()`?

Example 31

medium
Given `s = "hello world"`, return its number of words using split.

Example 32

medium
What does `"abc".replace("b", "X")` produce?

Example 33

medium
What does `"hello".find("l")` return?

Example 34

medium
Strip whitespace: `" hi ".strip()` returns?

Example 35

medium
Given `s = "AbCdEf"`, what is `s.lower()`?

Example 36

medium
Given `s = "abcd"`, what is `len(s[1:])`?

Example 37

medium
Does `"cat" == "Cat"`?

Example 38

medium
Given `s = "abc"`, in Python `s[0] = "z"` raises an error. Why?

Example 39

medium
Given `s = "data,science,2026"`, how many parts does `s.split(",")` return?

Example 40

hard
Given `s = "abcdef"`, what is `s[::-1][1:4]`?

Example 41

hard
Given `s = "abcabc"`, count occurrences of substring 'bc'.

Example 42

hard
Given `s = "AbCdE"`, count uppercase letters using a comprehension.

Example 43

challenge
Given `s = "abcde"`, write the slice that returns every other character starting at index 1.

Background Knowledge

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

assignmentdata types