Logic Building
# Take string input
s = input("Enter a string: ")
old_char = input("Enter character to replace: ")
new_char = input("Enter new character: ")
# Replace
result = s.replace(old_char, new_char)
print(f"After replacement: {result}")Output
Enter a string: Hello Enter character to replace: l Enter new character: x After replacement: Hexxo
Use replace() method to replace characters.
Key Concepts:
- replace(old, new) replaces all occurrences
- Returns new string
- Original unchanged