Iterate over words of a String in Python Last Updated : 10 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we’ll explore different ways to iterate over the words in a string using Python.Let's start with an example to iterate over words in a Python string: Python s = "Learning Python is fun" for word in s.split(): print(word) OutputLearning Python is fun Explanation: Split the string into words and loop through each wordTable of ContentUsing split() to Separate WordsIterating Directly with for LoopUsing enumerate to Track IndexHandling Extra Spaces and Empty WordsUsing split() to Separate WordsPython’s split() method allows us to split a string by whitespace and returns a list of words. Python s = "Learning Python is fun" words = s.split() print(words) Output['Learning', 'Python', 'is', 'fun'] Explanation: Here, split() divides the string at each whitespace, resulting in a list of words.Iterating Directly with for LoopOnce the string is split into words, we can iterate over each word using a for loop. Python s = "Learning Python is fun" for word in s.split(): print(word) OutputLearning Python is fun Explanation:s.split() returns ['Python', 'programming', 'is', 'powerful'].The for loop then iterates over this list, printing each word individually.Using enumerate to Track IndexTo keep track of the word position while iterating, we can use enumerate(). Python s = "Learning Python is fun" for index, word in enumerate(s.split()): print(f"Word {index + 1}: {word}") OutputWord 1: Learning Word 2: Python Word 3: is Word 4: fun Explanation: enumerate() provides a counter (index) with each iteration, which helps in keeping track of word positions.Handling Extra Spaces and Empty WordsSometimes strings may have extra spaces. split() handles this well. Python s = " Learning Python is fun " for word in s.split(): print(word) OutputLearning Python is fun Explanation: split() automatically removes extra spaces. Comment More infoAdvertise with us Next Article Iterate over words of a String in Python N nikhilaggarwal3 Follow Improve Article Tags : Python Python Programs python-string Python string-programs Practice Tags : python Similar Reads Iterate over characters of a string in Python In this article, we will learn how to iterate over the characters of a string in Python. There are several methods to do this, but we will focus on the most efficient one. The simplest way is to use a loop. Letâs explore this approach.Using for loopThe simplest way to iterate over the characters in 2 min read Python | Extract words from given string In Python, we sometimes come through situations where we require to get all the words present in the string, this can be a tedious task done using the native method. Hence having shorthand to perform this task is always useful. Additionally, this article also includes the cases in which punctuation 4 min read Python | Extract odd length words in String Sometimes, while working with Python, we can have a problem in which we need to extract certain length words from a string. This can be extraction of odd length words from the string. This can have application in many domains including day-day programming. Lets discuss certain ways in which this tas 5 min read Python - Words Lengths in String We are given a string we need to find length of each word in a given string. For example, we are s = "Hello world this is Python" we need to find length of each word so that output should be a list containing length of each words in sentence, so output in this case will be [5, 5, 4, 2, 6].Using List 2 min read Word location in String - Python Word location in String problem in Python involves finding the position of a specific word or substring within a given string. This problem can be approached using various methods in Python, such as using the find(), index() methods or by regular expressions with the re module.Using str.find()str.fi 4 min read Like