Find Longest String in Array
Find longest string in array of strings.
IntermediatePhase 6 - Category 2: Array & String CombinationsExample 4 of 10
find-longest-string-in-array.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# Find longest8if arr:9 longest = max(arr, key=len)10 print(f"Longest string: {longest}")11else:12 print("Array is empty")
Output
Enter number of strings: 3 String 1: Hello String 2: World String 3: Programming Longest string: Programming
What's going on
Use max() with key=len to find longest.
Key Concepts:
max() finds maximum
key=len uses length
Returns longest string
🔗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