0
0
PythonProgramBeginner · 2 min read

Python Program to Find Longest Word in String

You can find the longest word in a string in Python by splitting the string into words using split() and then using max(words, key=len) to get the longest word.
📋

Examples

InputI love programming
Outputprogramming
InputPython is fun
OutputPython
Inputa bb ccc dddd eeeee
Outputeeeee
🧠

How to Think About It

To find the longest word, first split the string into individual words by spaces. Then compare the length of each word to find which one is the longest. Return that word as the result.
📐

Algorithm

1
Get the input string.
2
Split the string into a list of words using spaces.
3
Find the word with the maximum length from the list.
4
Return or print the longest word.
💻

Code

python
text = input("Enter a string: ")
words = text.split()
longest_word = max(words, key=len)
print("Longest word:", longest_word)
Output
Enter a string: I love programming Longest word: programming
🔍

Dry Run

Let's trace the input 'I love programming' through the code

1

Input string

text = 'I love programming'

2

Split string into words

words = ['I', 'love', 'programming']

3

Find longest word

longest_word = 'programming' because it has length 11 which is longest

4

Print result

Output: Longest word: programming

WordLength
I1
love4
programming11
💡

Why This Works

Step 1: Splitting the string

Using split() breaks the string into words separated by spaces, making it easier to check each word.

Step 2: Finding the longest word

The max() function with key=len compares words by their length and returns the longest one.

Step 3: Outputting the result

Printing the longest word shows the user which word in the string is the longest.

🔄

Alternative Approaches

Using a loop to find longest word
python
text = input("Enter a string: ")
words = text.split()
longest_word = ''
for word in words:
    if len(word) > len(longest_word):
        longest_word = word
print("Longest word:", longest_word)
This method uses a manual loop to find the longest word, which is easy to understand but less concise than using max().
Using sorted() to get longest word
python
text = input("Enter a string: ")
words = text.split()
longest_word = sorted(words, key=len)[-1]
print("Longest word:", longest_word)
This sorts all words by length and picks the last one. It is less efficient because it sorts all words instead of just finding the max.

Complexity: O(n) time, O(n) space

Time Complexity

Splitting the string and checking each word length takes linear time proportional to the number of words, so O(n).

Space Complexity

Splitting creates a list of words, so space used is proportional to the number of words, O(n).

Which Approach is Fastest?

Using max() with key=len is fastest and most readable compared to sorting or manual loops.

ApproachTimeSpaceBest For
max() with key=lenO(n)O(n)Simple and efficient
Manual loopO(n)O(n)Clear logic for beginners
sorted() by lengthO(n log n)O(n)When sorted order is also needed
💡
Use max(words, key=len) for a simple and efficient way to find the longest word.
⚠️
Forgetting to split the string into words before trying to find the longest word.