In the previous article, we have discussed Python Program to Add Number to each Element in a List
Given a list of a string and some specific length and the task is to print items from a List with a given Specific Length.
len() function :
The len() function is a Python built-in function.
The length of a string is returned as an integer value by the len() function.
The length of a string can be obtained by passing a string value as a parameter to the len() function.
- Python Program for Printing Odd and Even Letters of a String
- Python Program to Calculate the Average of Numbers in a Given List
- Python Program to Find the First Capital Letter in a Given String
- BANKNIFTY Pivot Point Calculator
Examples:
Example1:
Input:
Given List = ['hello', 'btechgeeks', 'good', 'morning'] Given length = 5
Output:
The Items from a given List with given Specific Length : hello
Example 2:
Input:
Given List = ['abcd', 'efghigdh', 'kjfatr', 'ihg', 'dfth'] Given length = 4
Output:
The Items from a given List with given Specific Length : abcd dfth
Program to Print Items from a List with Specific Length
Below are the ways to print items from a List with a given Specific Length.
Method #1: Using For Loop (Static Input)
Approach:
- Give the List of String as static input and store it in a variable.
- Give the length as static input and store it in another variable.
- Loop in the above-given list using for loop.
- Inside the loop, check if the length of the iterator is equal to the given length using the len() function and if conditional statement.
- If the statement is true, then print the iterator value(i.e. string).
- The Exit of the program.
Below is the implementation:
# Give the List of String as static input and store it in a variable. gvn_lst = ['hello', 'btechgeeks', 'good', 'morning'] # Given the length as static input and store it in another variable. gvn_len = 5 # Loop in the above-given list using for loop. print("The Items from a given List with given Specific Length : ") for itr in gvn_lst: # Inside the loop, check if the length of the iterator is equal to the given length # using len() function and if conditional statement. if(len(itr)) == gvn_len: # If the statement is true, then print the iterator value(i.e. string). print(itr)
Output:
The Items from a given List with given Specific Length : hello
Method #2: Using For Loop (User Input)
Approach:
- Give the List of Strings as user input using list(),map(),input(),and split() functions and store it in a variable.
- Give the length as user input using the int(input()) function and store it in another variable.
- Loop in the above-given list using for loop.
- Inside the loop, check if the length of the iterator is equal to the given length using len() function and if conditional statement.
- If the statement is true, then print the iterator value(i.e. string).
- The Exit of the program.
Below is the implementation:
# Give the List of String as user input using list(),map(),input(),and #split() functions and store it in a variable. gvn_lst = list(map(str, input( 'Enter some random List Elements separated by spaces = ').split())) # Give the length as user input using int(input())and store it in another variable. gvn_len = int(input("Enter some random number = ")) # Loop in the above-given list using for loop. print("The Items from a given List with given Specific Length : ") for itr in gvn_lst: # Inside the loop, check if the length of the iterator is equal to the given length # using len() function and if conditional statement. if(len(itr)) == gvn_len: # If the statement is true, then print the iterator value(i.e. string). print(itr)
Output:
Enter some random List Elements separated by spaces = abcd efghigdh kjfatr ihg dfth Enter some random number = 4 The Items from a given List with given Specific Length : abcd dfth
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.