Coding Challenges: Strings Deep Dive
8 HackerRank-style challenges, auto-graded right in your browser —
no setup required. Write your function in the code box, then click
Run Tests to check it against a set of hidden-until-you-look
test cases (including edge cases like empty strings). Stuck? Every
challenge has a Show Solution reveal at the bottom.
Only uses what Chapters 1-6 have covered so far — no lists, no
def beyond the function stub already given, no
sorted()/sets.
1 Reverse a String Easy
Write a function reverse_string(s) that returns the string s reversed.
s may be empty, and may contain any mix of upper/lowercase letters.
reverse_string("hello") → "olleh"reverse_string("a") → "a"reverse_string("") → ""
Show Solution
def reverse_string(s):
return s[::-1]
2 Check a Palindrome Easy
Write a function is_palindrome(s) that returns True if s reads the same forwards and backwards, and False otherwise. This check is exact (case-sensitive, no trimming).
s may be empty (an empty string counts as a palindrome) or a single character (always a palindrome).
is_palindrome("racecar") → Trueis_palindrome("hello") → Falseis_palindrome("Aa") → False (case-sensitive)
Show Solution
def is_palindrome(s):
return s == s[::-1]
3 Count the Vowels Easy
Write a function count_vowels(s) that returns how many of the characters in s are vowels (a, e, i, o, u), counting both upper and lowercase.
s may be empty, and may mix upper/lowercase letters.
count_vowels("hello") → 2count_vowels("PYTHON") → 1count_vowels("") → 0
Show Solution
def count_vowels(s):
count = 0
for char in s.lower():
if char in "aeiou":
count = count + 1
return count
4 Count a Character Easy
Write a function count_char(s, ch) that returns how many times the single character ch appears in s.
s may be empty; ch may not appear in s at all (return 0).
count_char("banana", "a") → 3count_char("banana", "z") → 0count_char("Mississippi", "s") → 4
Show Solution
def count_char(s, ch):
return s.count(ch)
5 Is It a Valid Number String? Easy
Write a function is_valid_number(s) that returns True only if every character in s is a digit (0-9) — the kind of check you'd run before calling int(s) on form input.
s may be empty, contain a decimal point, a minus sign, or spaces — none of those count as valid.
is_valid_number("123") → Trueis_valid_number("12.5") → Falseis_valid_number("-5") → False
Show Solution
def is_valid_number(s):
return s.isdigit()
6 Capitalize Each Word Medium
Write a function capitalize_words(sentence) that returns sentence with the first letter of every word uppercase and the rest of each word lowercase, words separated by a single space. Build the result yourself with .split() and a for-loop — don't rely on .title() alone (it mishandles things like apostrophes).
sentence may be empty, a single word, or contain extra spaces between words (which .split() already collapses for you).
capitalize_words("hello world") → "Hello World"capitalize_words("PYTHON is FUN") → "Python Is Fun"capitalize_words("") → ""
Show Solution
def capitalize_words(sentence):
result = ""
first = True
for word in sentence.split():
piece = word[0].upper() + word[1:].lower()
if first:
result = piece
first = False
else:
result = result + " " + piece
return result
7 Mask a Card Number Medium
Write a function mask_string(s, keep) that replaces every character in s except the last keep characters with "*" — the way a receipt hides most of a credit card number.
keep may be 0 (mask everything) or greater than len(s) (nothing to mask — return s unchanged).
mask_string("4111111122223333", 4) → "************3333"mask_string("hello", 2) → "***lo"mask_string("hi", 4) → "hi"
Show Solution
def mask_string(s, keep):
masked_len = len(s) - keep
if masked_len < 0:
masked_len = 0
visible_start = len(s) - keep
if visible_start < 0:
visible_start = 0
return "*" * masked_len + s[visible_start:]
8 Anagram Check Medium
Write a function is_anagram(a, b) that returns True if a and b use exactly the same letters the same number of times (ignoring case and spaces), and False otherwise. Solve it using only .lower(), .replace(), len(), .count(), and a for-loop — no sorted(), lists, or sets.
is_anagram("listen", "silent") → Trueis_anagram("hello", "world") → Falseis_anagram("Dormitory", "Dirty Room") → True
Show Solution
def is_anagram(a, b):
a_clean = a.lower().replace(" ", "")
b_clean = b.lower().replace(" ", "")
if len(a_clean) != len(b_clean):
return False
for ch in a_clean:
if a_clean.count(ch) != b_clean.count(ch):
return False
return True