22

Phase 5 - Category 4: Character & Word Manipulation

Chapter 22 • Intermediate

90 min

Phase 5 - Category 4: Character & Word Manipulation

Introduction

Learn to manipulate strings by removing, replacing, and transforming characters and words based on various conditions.

Key Concepts

Character Removal

  • Remove Vowels: Delete all vowels
  • Remove Spaces: Delete all spaces
  • Remove Digits: Delete all digits
  • Remove Duplicates: Keep only first occurrence

Character Replacement

  • Replace Vowels: Replace with specific character
  • Replace Spaces: Replace with underscore or other
  • Swap Case: Convert uppercase to lowercase and vice versa
  • Shift Characters: Move characters by positions

Word Manipulation

  • Remove Duplicate Words: Keep unique words
  • Remove Consecutive Duplicates: Remove repeated words
  • Capitalize Words: First letter uppercase
  • Title Case: Each word capitalized

Common Operations

Remove Characters

python.js
result = ""
for char in text:
    if char not in to_remove:
        result += char

Replace Characters

python.js
result = text.replace(old_char, new_char)

Remove Duplicates

python.js
seen = set()
result = ""
for char in text:
    if char not in seen:
        result += char
        seen.add(char)

Problem-Solving Approach

  1. Iterate: Loop through characters or words
  2. Check Condition: Should character/word be kept or modified?
  3. Build Result: Construct new string
  4. Handle Edge Cases: Empty string, all same characters

Hands-on Examples

Remove Vowels

# Take string input
text = input("Enter a string: ")

# Remove vowels
vowels = "aeiouAEIOU"
result = ""
for char in text:
    if char not in vowels:
        result += char

print(f"String without vowels: {result}")

Iterate through string, add character to result only if it is not a vowel. Build new string character by character.