Python · Level 2 Text

String Methods

Real-world text is messy. Clean it up with Python's built-in string methods and build new text with f-strings.

  • lower(), strip(), split() and join()
  • Checking characters with isalnum() and isspace()
  • f-strings for formatting

Strings come with methods that return a new string — the original never changes. Chain them to clean text step by step.

name = "  ada LOVELACE "
clean = name.strip().title()   # 'Ada Lovelace'
words = clean.split()          # ['Ada', 'Lovelace']
"-".join(words)                # 'Ada-Lovelace'
"py3".isalnum()                # True
f"Hi {clean}, {len(words)} names"

Write slugify(title) that turns a title into a URL slug: lowercase, words joined by single hyphens, and every character that isn't a letter, digit or space removed.

solution.py
Test Suites Pending
Stdout