Sum of Numbers in Array of Strings
Extract and sum all numbers from array of strings.
AdvancedPhase 6 - Category 2: Array & String CombinationsExample 6 of 10
sum-of-numbers-in-array-of-strings.py
1# Take array of strings2n = int(input("Enter number of strings: "))3arr = []4for i in range(n):5 arr.append(input(f"String {i+1}: "))67# Extract and sum numbers8total = 09for s in arr:10 current_num = ""11 for char in s:12 if char.isdigit():13 current_num += char14 else:15 if current_num:16 total += int(current_num)17 current_num = ""18 if current_num:19 total += int(current_num)2021print(f"Total sum: {total}")
Output
Enter number of strings: 3 String 1: Hello123 String 2: World456 String 3: Test789 Total sum: 1368
What's going on
Extract numbers from each string and sum.
Key Concepts:
Extract numbers from strings
Convert to integers
Accumulate sum
🔗Related Content
- 📖
Phase 6 - Learn Concepts
Review Phase 6 concepts and explanations
- 📝
Phase 6 - Quiz
Test your Phase 6 understanding with quiz questions
- 💻
Phase 6 - All Practice Problems
Explore all practice problems for Phase 6
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum