ADVERTISEMENT
ADVERTISEMENT

Python program to find words which are greater than given length k

Here, we are going to learn how to find words which are greater than given length k in Python?
Submitted by Shivang Yadav, on April 17, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Strings in Python are immutable means they cannot be changed once defined.

Finding words which are greater than given length k

We will take a string of words from the user along with an integer k. We will find all words whose length is greater than k.

Example:

Input:
"learn programming at includehelp", k = 6
Output:
programming, includehelp

As we know that each word of the string is separated by spaces. So, we will traverse the string and extract each word by splitting between spaces. Then, for each word we will count lengths and print all words whose length is greater than k.

Program to find words which are greater than given length

# Python program to find words which are greater 
# than given length k

# Getting input from user 
myStr =  input('Enter the string : ')
k = int(input('Enter k  (value for accepting string) : '))
largerStrings = []

# Finding words with length greater than k
words = myStr.split(" ")
for word in words:
	if len(word) > k:
		largerStrings.append(word)
		
# printing values
print("All words which are greater than given length ", k, "are ", largerStrings)

Output:

Enter the string : learn python programming at include help
Enter k  (value for accepting string) : 5 6
All words which are greater than given length  6 are  ['programming', 'include']

Explanation:

In the above code, we have taken a string myStr and integer k. Then we will create a collection of words from the string named words using the split method. Then find all words with a length greater than k in a collection called largerString. Then print the value of largerString.

Python String Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.