Skip to content

ENUMERATE WITH STRING MANIPULATION

Python
#!/usr/bin/env python3
"""
ENUMERATE WITH STRING MANIPULATION
Process strings with position awareness
"""

print("=== Add Position Markers to String ===")
text = "Python"

for i, char in enumerate(text):
    print(f"Position {i}: '{char}'")

print("\n=== Alternate Case by Position ===")
word = "programming"

alternating = ''.join(char.upper() if i % 2 == 0 else char for i, char in enumerate(word))
print(f"Original: {word}")
print(f"Alternating case: {alternating}")

print("\n=== Find Character Positions ===")
sentence = "hello world"
target = 'o'

positions = [i for i, char in enumerate(sentence) if char == target]
print(f"Character '{target}' found at positions: {positions}")

print("\n=== Censor Every Nth Character ===")
message = "This is a secret message"
n = 3

censored = ''.join('*' if i % n == 0 else char for i, char in enumerate(message))
print(f"Original: {message}")
print(f"Censored (every {n}rd char): {censored}")

print("\n=== Build Indexed Word List ===")
text = "the quick brown fox jumps"
words = text.split()

indexed = {i: word for i, word in enumerate(words)}
print(f"Indexed words: {indexed}")