Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding Words Lengths in String using Python
Finding the lengths of individual words in a string is a common task in text processing and data analysis. Python provides several approaches to accomplish this, from simple loops to more advanced techniques using regular expressions and dictionaries.
Methods Used
Using a loop and the
split()functionUsing the
map()function withlenandsplit()Using the
re.split()method from the re moduleUsing a Dictionary to store word lengths
Using a Loop and the split() Function
This is the most straightforward approach. The split() function tokenizes the input text based on whitespace characters, and we iterate through the resulting words to calculate their lengths ?
Algorithm
Split the input string into words using
split()Initialize an empty list to store word lengths
Loop through each word and calculate its length using
len()Return the list of word lengths
Example
def find_word_lengths(input_string):
words = input_string.split()
word_lengths = []
for word in words:
word_lengths.append(len(word))
return word_lengths
input_string = "Today feels like a productive day"
result = find_word_lengths(input_string)
print("The word lengths are:", result)
The word lengths are: [5, 5, 4, 1, 10, 3]
Using the map() Function with len and split()
The map() function provides a more concise approach by applying the len() function to each word in the split list ?
Example
def find_word_lengths(input_string):
words = input_string.split()
word_lengths = list(map(len, words))
return word_lengths
input_string = "Hello my name is Rahul"
result = find_word_lengths(input_string)
print("The word lengths are:", result)
The word lengths are: [5, 2, 4, 2, 5]
Using the re.split() Method
Regular expressions provide more control over how text is split. The pattern r'\s+' matches one or more consecutive whitespace characters, making it robust for handling various whitespace patterns ?
Example
import re
def word_lengths(sentence):
words = re.split(r'\s+', sentence)
word_lengths_list = []
for word in words:
if word: # Skip empty strings
word_lengths_list.append(len(word))
return word_lengths_list
sentence = "This is a sample sentence"
result = word_lengths(sentence)
print("Word lengths:", result)
Word lengths: [4, 2, 1, 6, 8]
Using a Dictionary to Store Word Lengths
This method creates a dictionary where each word is a key and its length is the corresponding value. This approach is useful when you need to look up the length of specific words ?
Example
def word_lengths(sentence):
words = sentence.split()
word_lengths_dict = {}
for word in words:
word_lengths_dict[word] = len(word)
return word_lengths_dict
sentence = "This is how the length of words is calculated"
result = word_lengths(sentence)
print("Word lengths dictionary:", result)
Word lengths dictionary: {'This': 4, 'is': 2, 'how': 3, 'the': 3, 'length': 6, 'of': 2, 'words': 5, 'calculated': 10}
Comparison
| Method | Best For | Output Format | Memory Usage |
|---|---|---|---|
| Loop with split() | Simple, readable code | List of lengths | Medium |
| map() with len | Concise, functional style | List of lengths | Low |
| re.split() | Complex whitespace patterns | List of lengths | Medium |
| Dictionary | Word-to-length mapping | Dictionary | High |
Conclusion
Use map() with len for concise code, regular expressions for complex text patterns, and dictionaries when you need to map words to their lengths. For simple tasks, the basic loop approach offers the best readability.
