Check if All Characters Same
Check if all characters in string are same.
IntermediatePhase 5 - Category 5: Advanced String LogicExample 6 of 14
check-if-all-characters-same.py
1# Take string input2s = input("Enter a string: ")34# Check if all same5if len(s) == 0:6 print("Empty string")7elif len(set(s)) == 1:8 print("All characters are same")9else:10 print("Characters are different")
Output
Enter a string: aaaa All characters are same Enter a string: hello Characters are different
What's going on
Use set to check unique characters.
Key Concepts:
set(s) gives unique characters
If length is 1, all same
Otherwise, different
🔗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