Phase 5 - Category 4: String Transformation
Transform strings - replace, remove, insert operations.
10 programs · open one, then the next
Start with Replace Character- 01Replace CharacterIntermediateReplace matching text with str.replace. Students read a target and replacement, preserve the source string, and learn that the API accepts substrings too.Open example →py# Take string input s = input("Enter a string: ") old_char = input("Enter character to replace: ") new_char = input("Enter new character: ")
- 02Remove CharacterIntermediateRemove chosen text with replace and an empty replacement. Students distinguish deleting all matching substrings from deleting a character at a particular index.Open example →py# Take string input s = input("Enter a string: ") char_to_remove = input("Enter character to remove: ")
- 03Insert Character at PositionIntermediateInsert text at a validated string position using slices. Students join prefix, inserted text, and suffix and include the position just after the character.Open example →py# Take string input s = input("Enter a string: ") char = input("Enter character to insert: ") pos = int(input("Enter position: "))
- 04Remove First CharacterBeginnerRemove the first character of text with slicing. Students guard empty input, retain the remaining sequence, and see why a one-character result becomes empty.Open example →py# Take string input s = input("Enter a string: ") # Remove first character
- 05Remove Last CharacterBeginnerDrop the character with a slice. Students trace an excluded negative endpoint and distinguish removing one position from stripping repeated trailing characters.Open example →py# Take string input s = input("Enter a string: ") # Remove last character
- 06Swap First and Last CharacterIntermediateSwap a string's endpoint characters with concatenation and slicing. Students retain the middle substring and require at least two positions before swapping.Open example →py# Take string input s = input("Enter a string: ") # Swap first and last
- 07Duplicate String N TimesBeginnerRepeat a string n times with multiplication. Students distinguish numeric multiplication from text repetition and test zero or negative repeat counts honestly.Open example →py# Take string input s = input("Enter a string: ") n = int(input("Enter number of times: "))
- 08Add Characters Between Each CharacterIntermediateInsert a separator between string characters with join. Students treat a string as an iterable of characters and avoid adding separators at either endpoint.Open example →py# Take string input s = input("Enter a string: ") char = input("Enter character to add: ")
- 09Remove All Occurrences of SubstringIntermediateRemove all nonoverlapping substring matches with Python replace. Students preserve unmatched text and distinguish literal matching from regular expressions.Open example →py# Take string input s = input("Enter a string: ") substring = input("Enter substring to remove: ")
- 10Reverse Each WordIntermediateReverse the letters inside each word while keeping word order. Students combine split, a list comprehension, character slicing, and a final single-space join.Open example →py# Take string input s = input("Enter a string: ") # Reverse each word