Replace Character
Replace all occurrences of a character.
IntermediatePhase 5 - Category 4: String TransformationExample 1 of 10
replace-character.py
1# Take string input2s = input("Enter a string: ")3old_char = input("Enter character to replace: ")4new_char = input("Enter new character: ")56# Replace7result = s.replace(old_char, new_char)8print(f"After replacement: {result}")
Output
Enter a string: Hello Enter character to replace: l Enter new character: x After replacement: Hexxo
What's going on
Use replace() method to replace characters.
Key Concepts:
replace(old, new) replaces all occurrences
Returns new string
Original unchanged
🔗Related Content
- 📖
Phase 5 - Learn Concepts
Review Phase 5 concepts and explanations
- 📝
Phase 5 - Quiz
Test your Phase 5 understanding with quiz questions
- 💻
Phase 5 - All Practice Problems
Explore all practice problems for Phase 5
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum