Practice String in CS Thinking

Use these practice problems to test your method after reviewing the concept explanation and worked examples.

Quick 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.

Showing a random 20 of 50 problems.

Example 1

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

Example 2

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

Example 3

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

Example 4

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

Example 5

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

Example 6

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

Example 7

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

Example 8

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

Example 9

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

Example 10

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

Example 11

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

Example 12

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

Example 13

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

Example 14

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

Example 15

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

Example 16

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

Example 17

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

Example 18

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

Example 19

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

Example 20

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