Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Reverse Words in a Given String in Python
Next article icon

Python program to print even length words in a string

Last Updated : 04 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of printing even-length words from a string in Python involves identifying and extracting words whose lengths are divisible by 2. Given an input string, the goal is to filter out words with an even number of characters and display them. For example , s = “Python is great”, the even-length words are [“Python”, “is”] with lengths [6, 2]. “Python” (6) and “is” (2) are included as their lengths are even, while “great” (5) is excluded as it is odd.

Using list comprehension

This method splits the string into words, filters out even-length words using list comprehension and joins them into a single output string. It is the fastest and cleanest approach, as it avoids multiple print calls and unnecessary loops.

Python
s = "This is a python language"  # split the sentence into words wrds = s.split()  # filter words with even length even_wrds = [w for w in wrds if len(w) % 2 == 0]  # Join the filtered words back into a sentence res = " ".join(even_wrds) print(res) 

Output
This is python language 

Explanation: s is split into words using .split(), creating a list. List comprehension then filters words with even lengths (len(w) % 2 == 0) into even_wrds. Finally, join() combines these words into a space-separated string .

Table of Content

  • Using filter()
  • Using generator expression with map
  • Using for loop

Using filter()

This approach uses filter() to extract even-length words and lambda to define the condition, making it a functional programming alternative. The join() function combines the words into a single output.

Python
s = "Python is great"  # split the sentence into words wrds = s.split()  # filter words with even length even_wrds = filter(lambda w: len(w) % 2 == 0, wrds)  # join the words back into a string res = " ".join(even_wrds)  print(res) 

Output
Python is 

Explanation: filter() with a lambda (len(w) % 2 == 0) extracts even-length words, which are then joined into a space-separated string using join().

Using generator expression with map

A memory-efficient approach, this method generates words dynamically instead of storing them in a list. It filters even-length words on the fly using a generator expression and then joins them efficiently.

Python
a = "geeks for geek"  # split the sentence into words wrds = a.split()  # filter words with even length even_wrds = (w for w in wrds if len(w) % 2 == 0)  # convert words to strings even_wrd_str = map(str, even_wrds)  # join the words back into a sentence res = " ".join(even_wrd_str)  print(res) 

Output
geek 

Explanation: generator expression filters even-length words dynamically, map(str, even_wrds) ensures they are strings and join() combines them into a space-separated string.

Using for loop

A simple, beginner-friendly approach is using a for loop. This method iterates through each word, checks its length and prints it if it’s even. While not the most optimized, it is easy to understand and implement.

Python
a = "My name is shakshi"  # split the sentence into words wrds = a.split()  # loop through each word and check if its length is even for w in wrds:     if len(w) % 2 == 0:         print(w, end=" ") 

Output
My name is 

Explanation: for loop iterates through each word, checking if its length is even (len(w) % 2 == 0). If true, the word is printed with a space separator using end=” “.



Next Article
Reverse Words in a Given String in Python

M

MuskanChoudhary
Improve
Article Tags :
  • Python
Practice Tags :
  • python

Similar Reads

  • Reverse Words in a Given String in Python
    In this article, we explore various ways to reverse the words in a string using Python. From simple built-in methods to advanced techniques like recursion and stacks. We are going to see various techniques to reverse a string. Using split() and join()Using split() and join() is the most common metho
    2 min read
  • Convert string to a list in Python
    Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
    2 min read
  • Convert integer to string in Python
    In this article, we’ll explore different methods for converting an integer to a string in Python. The most straightforward approach is using the str() function. Using str() Functionstr() function is the simplest and most commonly used method to convert an integer to a string. [GFGTABS] Python n = 42
    2 min read
  • Add padding to a string in Python
    Padding a string involves adding extra characters such as spaces or specific symbols to adjust its length or align it in a visually appealing way. Let’s dive into the most efficient methods to add padding to a string in Python. Using Python f-stringsF-strings allow us to specify padding directly wit
    2 min read
  • String Alignment in Python f-string
    String alignment in Python helps make text look neat and organized, especially when printing data of different lengths. Without formatting, output can appear messy and hard to read. Python’s f-strings make it easy to align text by controlling its position within a set space. Python’s f-strings allow
    3 min read
  • Output of Python Programs | Set 23 (String in loops)
    Prerequisite: Loops and String Note: Output of all these programs is tested on Python3 1. What is the output of the following? my_string = "geeksforgeeks" i = "i" while i in my_string: print(i, end =" ") None geeksforgeeks i i i i i i … g e e k s f o r g e e k s Output:
    2 min read
  • Convert string to title case in Python
    In this article, we will see how to convert the string to a title case in Python. The str.title() method capitalizes the first letter of every word. [GFGTABS] Python s = "geeks for geeks" result = s.title() print(result) [/GFGTABS]OutputGeeks For Geeks Explanation: The s.title() method con
    2 min read
  • Python | Words extraction from set of characters using dictionary
    Given the words, the task is to extract different words from a set of characters using the defined dictionary. Approach: Python in its language defines an inbuilt module enchant which handles certain operations related to words. In the approach mentioned, following methods are used. check() : It che
    3 min read
  • Python - Separate first word from String
    We need to write a Python program to split a given string into two parts at the Kᵗʰ occurrence of a specified character. If the character occurs fewer than K times, return the entire string as the first part and an empty string as the second part. Separating the first word from a string involves ide
    2 min read
  • How to Remove Letters From a String in Python
    Removing letters or specific characters from a string in Python can be done in several ways. But Python strings are immutable, so removal operations cannot be performed in-place. Instead, they require creating a new string, which uses additional memory. Let’s start with a simple method to remove a s
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences