Print Characters Recursively
Print each character of string recursively.
AdvancedPhase 3 - Level 4: String-based RecursionExample 7 of 10
print-characters-recursively.py
1def print_chars(s, index=0):2 # Base case3 if index >= len(s):4 return56 # Print current character7 print(s[index])8 # Recurse9 print_chars(s, index + 1)1011# Test12text = input("Enter a string: ")13print_chars(text)
Output
Enter a string: Hello H e l l o
What's going on
Print current character, recurse for next.
Key Concepts:
Base case: index >= len(s)
Print character at index
Recurse with index + 1
🔗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