Sum of Numbers in String
Calculate sum of all numbers in string.
AdvancedPhase 5 - Category 5: Advanced String LogicExample 5 of 14
sum-of-numbers-in-string.py
1# Take string input2s = input("Enter a string: ")34# Extract and sum numbers5total = 06current_num = ""7for char in s:8 if char.isdigit():9 current_num += char10 else:11 if current_num:12 total += int(current_num)13 current_num = ""14if current_num:15 total += int(current_num)1617print(f"Sum: {total}")
Output
Enter a string: Hello123World456 Sum: 579
What's going on
Extract numbers and accumulate sum.
Key Concepts:
Extract numbers from string
Convert to integers
Sum all numbers
🔗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