
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Find words greater than given length
When it is required to find words that are greater than a specific length, a method is defined that splits the string, and iterates through it. It checks the length of the word and compares it with the given length. If they match, it is returned as output.
Example
Below is a demonstration of the same
def string_check(string_length, my_string): result_string = [] words = my_string.split(" ") for x in words: if len(x) > string_length: result_string.append(x) return result_string string_length = 3 my_string ="Python is always fun to learn" print("The string is :") print(my_string) print "\nThe words in the string with length greater than" , string_length , "is :" print(string_check(string_length, my_string))
Output
The string is : Python is always fun to learn The words in the string with length greater than 3 is : ['Python', 'always', 'learn']
Explanation
A method named ‘string_check’ is defined that takes the string, and its length as parameter.
An empty list is defined.
The string is split based on spaces, and assigned to a variable.
This variable is iterated over, and the given length and the length of the each word is checked.
If the length of the word is greater than the length of the string, it is appended to the empty string.
It is returned as output.
Outside the function, a string length is defined, and a string is defined.
This string is displayed on the console.
The method is called and the output is displayed on the console.
- Related Articles
- Find Strings greater than a particular length in MongoDB?
- Program to find lowest sum of pairs greater than given target in Python
- Python Program to find out the number of sets greater than a given value
- Find Smallest Letter Greater Than Target in Python
- Find smallest element greater than K in Python
- Find the Next perfect square greater than a given number in C++
- Find Elements Greater Than a Given Number In a Subarray in Java
- Python – Remove characters greater than K
- Find k longest words in given list in Python
- How to find the smallest number greater than x in Python?
- Program to find maximum length of non-sharing words in Python
- Python Indices of numbers greater than K
- Python – Average of digit greater than K
- Python – Filter Tuples Product greater than K
- Count the number of words having sum of ASCII values less than and greater than k in C++
