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?
By Shivang Yadav Last updated : March 04, 2024

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.

To understand the above program, you should have the basic knowledge of the following Python topics:

Python String Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.