23

Phase 5 - Category 5: Word-level Operations

Chapter 23 • Advanced

90 min

Phase 5 - Category 5: Word-level Operations

Introduction

This advanced level focuses on word-level string operations. You'll analyze words, manipulate them, and extract information at the word level.

Key Concepts

Word Analysis

  • Longest Word: Find word with maximum length
  • Shortest Word: Find word with minimum length
  • Word Count: Count total words
  • Even-length Words: Count words with even number of characters

Word Manipulation

  • Capitalize First Letter: First letter of each word uppercase
  • Title Case: Each word properly capitalized
  • Swap First & Last Word: Exchange positions
  • Remove Extra Spaces: Clean up spacing

Word Filtering

  • Words Starting with Vowel: Filter by first character
  • Words Ending with Specific Letter: Filter by last character
  • Words Containing Character: Filter by presence
  • Words with Same Start/End: Find words where first and last letter same

Common Operations

Split into Words

python.js
words = text.split()

Find Longest Word

python.js
longest = max(words, key=len)

Capitalize Words

python.js
capitalized = [word.capitalize() for word in words]

Problem-Solving Approach

  1. Split: Convert string to list of words
  2. Iterate: Process each word
  3. Analyze: Check word properties
  4. Transform: Modify words as needed
  5. Join: Combine words back to string

Hands-on Examples

Find Longest Word

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

# Split into words
words = text.split()

if len(words) == 0:
    print("No words found")
else:
    # Find longest word
    longest = words[0]
    for word in words:
        if len(word) > len(longest):
            longest = word
    
    print(f"Longest word: {longest}")
    print(f"Length: {len(longest)}")

Split string into words, iterate through words, track word with maximum length. Compare length of each word with current longest.