Print Last Word
Print last word of string.
BeginnerPhase 5 - Category 3: Word OperationsExample 2 of 10
print-last-word.py
1# Take string input2s = input("Enter a string: ")34# Get last word5words = s.split()6if len(words) > 0:7 print(f"Last word: {words[-1]}")8else:9 print("No words found")
Output
Enter a string: Hello World Programming Last word: Programming
What's going on
Split string and get last element.
Key Concepts:
split() splits into words
words[-1] is last word
Negative indexing
🔗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