Finding Longest Word in Paragraph using Python
- Posted by Admin
- on August 23, 2022
- in Python
- No Comments.
Post Views: 1,355
Here is the input paragraph we have.
# Input Paragraph text = ''' Computer vision is a field of artificial intelligence (AI) that enables computers and systems to derive meaningful information from digital images, videos and other visual inputs — and take actions or make recommendations based on that information. '''
We need to find the longest word out of above paragraph.
The output should be: recommendations
Solution: Method 1
# Input Paragraph
text = '''
Computer vision is a field of artificial
intelligence (AI) that enables computers and systems
to derive meaningful information from digital images,
videos and other visual inputs — and take actions or make
recommendations based on that information.
'''
# We can remove unwanted items or text
cleaned_text = text.replace(',','')
cleaned_text = cleaned_text.replace('\n','')
# make list of words - Split the paragraph with whitespace
word_list = cleaned_text.split(' ')
# Sort the list with its length using Key
sorted_wordlist = sorted(word_list, key=len)
# Show Longest word
print(sorted_wordlist[-1]) # Output -> recommendations
# We can show the length as well
print(sorted_wordlist[-1], '(', len(sorted_wordlist[-1]),')') # Output -> recommendations ( 15 )Solution: Method 1
# Input Paragraph
text = '''
Computer vision is a field of artificial
intelligence (AI) that enables computers and systems
to derive meaningful information from digital images,
videos and other visual inputs — and take actions or make
recommendations based on that information.
'''
# We can remove unwanted items or text
cleaned_text = text.replace(',','')
cleaned_text = cleaned_text.replace('\n','')
# make list of words - Split the paragraph with whitespace
word_list = cleaned_text.split(' ')
# Sort the list with its length using Key
sorted_wordlist = sorted(word_list, key=len)
# Show Longest word
print(sorted_wordlist[-1]) # Output -> recommendations
# We can show the length as well
print(sorted_wordlist[-1], '(', len(sorted_wordlist[-1]),')') # Output -> recommendations ( 15 )Solution: Method 2
# Input Paragraph
text = '''
Computer vision is a field of artificial
intelligence (AI) that enables computers and systems
to derive meaningful information from digital images,
videos and other visual inputs — and take actions or make
recommendations based on that information.
'''
# We can remove unwanted items or text
cleaned_text = text.replace(',','')
cleaned_text = cleaned_text.replace('\n','')
# make list of words - Split the paragraph with whitespace
word_list = cleaned_text.split(' ')
# with its length using Key
longest_word = max(word_list, key=len)
# Show Longest word
print(longest_word) # Output -> recommendations
# We can show the length as well
print(longest_word, '(', len(longest_word),')') # Output -> recommendations ( 15 )
Post Tagged with : filter largest word in string, finding longest keyword, finding longest word in paragraph using python, key, python
