Extract Numbers from String
Extract all numbers from string.
AdvancedPhase 5 - Category 5: Advanced String LogicExample 4 of 14
extract-numbers-from-string.py
1# Take string input2s = input("Enter a string: ")34# Extract numbers5numbers = []6current_num = ""7for char in s:8 if char.isdigit():9 current_num += char10 else:11 if current_num:12 numbers.append(int(current_num))13 current_num = ""14if current_num:15 numbers.append(int(current_num))1617print(f"Numbers: {numbers}")
Output
Enter a string: Hello123World456 Numbers: [123, 456]
What's going on
Build numbers character by character.
Key Concepts:
Track current number being built
Add digit to current number
Convert to int when complete
🔗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