Remove Spaces Recursively
Remove all spaces from string recursively.
AdvancedPhase 3 - Level 4: String-based RecursionExample 4 of 10
remove-spaces-recursively.py
1def remove_spaces(s):2 # Base case3 if len(s) == 0:4 return ""56 # Check first character7 if s[0] == ' ':8 return remove_spaces(s[1:])9 else:10 return s[0] + remove_spaces(s[1:])1112# Test13text = input("Enter a string: ")14result = remove_spaces(text)15print(f"Without spaces: {result}")
Output
Enter a string: Hello World Without spaces: HelloWorld
What's going on
Skip space, include other characters.
Key Concepts:
Base case: empty string
If space, skip and recurse
Otherwise, include and recurse
🔗Related Content
- 📖
Phase 3 - Learn Concepts
Review Phase 3 concepts and explanations
- 📝
Phase 3 - Quiz
Test your Phase 3 understanding with quiz questions
- 💻
Phase 3 - All Practice Problems
Explore all practice problems for Phase 3
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum