Q:

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

belongs to collection: Python String Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to check if a string contains special characte... >>
<< Python program to split and join a string...