MASTER YOUR LOGIC BUILDING:Phase 5 - Category 4: Character & Word Manipulation

Mastering phase 5 - category 4: character & word manipulation concepts and implementation.

Character & Word Manipulation

Why This Matters

Text editing is everywhere — autocorrect removes typos, filters hide bad words, forms strip extra spaces. When you remove vowels, swap letter cases, or drop duplicate characters, you're building a new string piece by piece based on rules. This chapter teaches you that "transform text" mindset.

What You'll Learn

  • How to remove specific characters (vowels, digits, spaces)
  • How to replace characters and swap uppercase ↔ lowercase
  • How to remove duplicate characters while keeping first occurrence
  • The "build a new string in a loop" pattern used in most manipulation problems

The Golden Pattern: Filter and Build

Since strings can't be changed in place, you create a new string by deciding what to keep:

text = "Hello"
vowels = "aeiouAEIOU"
result = ""

for char in text:
    if char not in vowels:
        result += char   # keep consonants and spaces

print(result)   # "Hll"

Ask yourself for every character: Should this go in my new string? Yes → append it. No → skip it.

Remove vs Replace

Remove — skip unwanted characters (shown above)

Replace — swap one character for another:

text = "hello world"
result = text.replace(" ", "_")
print(result)   # "hello_world"

Replace with logic — different rules for different characters:

text = "HeLLo"
result = ""
for char in text:
    if char.isupper():
        result += char.lower()
    elif char.islower():
        result += char.upper()
    else:
        result += char

print(result)   # "hEllO"

Step-by-Step: Remove Duplicate Characters

Keep only the first time you see each character: "programming" → "progamin"

  1. Create an empty seen set to track characters you've already added
  2. Loop through the string
  3. If character not in seen, add it to result and mark as seen
text = "banana"
seen = set()
result = ""

for char in text:
    if char not in seen:
        result += char
        seen.add(char)

print(result)   # "ban"

Word-Level Manipulation

Split first, work on words, join back:

sentence = "hello world hello"
words = sentence.split()          # ['hello', 'world', 'hello']
unique = []

for word in words:
    if word not in unique:
        unique.append(word)

print(' '.join(unique))   # "hello world"

Common Mistakes & Tips

  • Trying to delete from a string — Python strings don't support removal; always build a new one
  • Forgetting non-letters in swap-case — digits and punctuation should usually stay unchanged
  • Removing duplicates in wrong order — use a set for tracking, but build result in loop order to preserve first occurrence
  • Tip: Write the expected output on paper first, then trace your loop character by character

Practice Focus

  • Remove all vowels from a sentence
  • Replace every space with a hyphen
  • Swap the case of every letter in a string
  • Remove duplicate characters from a word
  • Remove all digits from a mixed string like "abc123def"

These "build a new string" skills combine perfectly with counting and reversing from earlier chapters!

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.