Swap First and Last Character
Swap first and last character.
IntermediatePhase 5 - Category 4: String TransformationExample 6 of 10
swap-first-and-last-character.py
1# Take string input2s = input("Enter a string: ")34# Swap first and last5if len(s) >= 2:6 result = s[-1] + s[1:-1] + s[0]7 print(f"After swap: {result}")8else:9 print("String too short")
Output
Enter a string: Hello After swap: oellH
What's going on
Reconstruct string with swapped characters.
Key Concepts:
s[-1] is last character
s[1:-1] is middle part
s[0] is first character
🔗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