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
hardGiven `s = "abcdef"`, what is `s[::-1][1:4]`?
Example 2
mediumWhat does `"abc".replace("b", "X")` produce?
Example 3
easyHow many characters are in `"a b"` (with the space)?
Example 4
challengeBuild the substring of `"computer"` from index 2 up to but not including the last character. What is it?
Example 5
easyWhat is the length of `"science"`?
Example 6
easyRepeat a string: what is `"ab" * 3`?
Example 7
hardWrite a one-liner check: is `"level"` a palindrome?
Example 8
challengeCheck if `"racecar"` is a palindrome by comparing it to its reverse. Result?
Example 9
hardGiven `s = "AbCdE"`, count uppercase letters using a comprehension.
Example 10
mediumGiven `s = "abcde"`, what is `s[1:4]`?
Example 11
easyGiven `s = "data"`, what is `s[-1]` (last character)?
Example 12
easyWhat is the length of `"world"`?
Example 13
easyGiven `s = "cat"`, what is `s[2]`?
Example 14
mediumGiven `s = "abc"`, in Python `s[0] = "z"` raises an error. Why?
Example 15
hardGiven `s = "abcabc"`, count occurrences of substring 'bc'.
Example 16
easyGiven `s = "sun"`, what is `s[-1]`?
Example 17
mediumWhat is the result of concatenating 'Score: ' + STRING(95) + '%'?
Example 18
mediumBuild a greeting: `name = "Sam"`; produce `"Hello, Sam!"` using concatenation.
Example 19
challengeGiven `s = "abcdefg"`, what does `s[1:6:2]` produce?
Example 20
mediumStrip whitespace: `" hi ".strip()` returns?