From the course: Learning Python

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Solution: Find the longest string

Solution: Find the longest string - Python Tutorial

From the course: Learning Python

Solution: Find the longest string

(upbeat music) - [Instructor] For this challenge we needed to find the longest string in a list of strings and then return that length as the result. In order to solve this challenge your code needed to do three things. So first, you have to loop, or iterate over each of the strings in the list, that is passed to the function as a parameter. Next, you have to calculate the length of each string, and then finally, you had to return the length of the longest string. So here, in my solution, my code uses a for loop to iterate over each string and we learned about forms back in chapter two. And they use this temporary variable named s. To calculate the length of each string I use the built-in len function which we also learned about earlier, and that returned the length of the string. My code uses this temporary variable named maxlen to hold the length of the longest string that we've seen so far which is initialized to zero. So if the length of the current string is larger than the…

Contents