4.2.14 Practical Application: Anagram Detection

Unit: Unit 4: Strings, Lists, and Tuples • Interactive Practice

Quick Answer & Summary:

Interactive Python programming tutorial and practice in your browser.

Interactive Challenge (Run Live in Browser):

# Determine if two candidate strings are anagrams
word_a = "Conversation"
word_b = "Voices, rant on"

# Clean strings: keep only lowercase letters
clean_a = [ch.lower() for ch in word_a if ch.isalpha()]
clean_b = [ch.lower() for ch in word_b if ch.isalpha()]

# Anagram test: compare sorted character lists
is_anagram = sorted(clean_a) == sorted(clean_b)

print("Cleaned List A:", clean_a)
print("Cleaned List B:", clean_b)
print("Are they anagrams?", is_anagram)
Open in Live Interactive Python Sandbox →