Count Substring Occurrences
Count how many times substring appears.
IntermediatePhase 5 - Category 5: Advanced String LogicExample 3 of 14
count-substring-occurrences.py
1# Take two strings2s = input("Enter string: ")3substring = input("Enter substring: ")45# Count occurrences6count = s.count(substring)7print(f"Occurrences: {count}")
Output
Enter string: Hello Hello World Enter substring: Hello Occurrences: 2
What's going on
Use count() method to count occurrences.
Key Concepts:
count(substring) counts occurrences
Returns number of matches
Simple method
🔗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