Open In App

Python | Words lengths in String

Improve
Improve
Like Article
Like
Save
Share
Report

We sometimes come through situations where we require to get all the word lengths present in the string, this can be a tedious task done using a naive method. Hence having shorthand to perform this task is always useful. Let’s discuss certain ways to achieve this. 

Method #1 : Using split() + len()

Using the split function, we can split the string into a list of words which is the most generic and recommended method if one wished to accomplish this particular task. But the drawback is that it fails in the cases in which string contains punctuation marks. The len() is used to compute string length. 

Python3




# Python3 code to demonstrate Words lengths in String
# using split() method
 
# Initializing string
test_string = "Geeksforgeeks is best Computer Science Portal"
 
# Printing original string
print("The original string is : " + test_string)
 
# Words lengths in String
# using split() method
res = list(map(len, test_string.split()))
 
# Printing result
print("The list of words lengths is : " + str(res))


Output : 

The original string is : Geeksforgeeks is best Computer Science Portal
The list of words lengths is : [13, 2, 4, 8, 7, 6]

Time complexity: O(n)
Auxiliary Space: O(n), where n is length of string.

Method #2: Using regex( findall()  + len()

In the cases which contain all the special characters and punctuation marks, as discussed above, the conventional method of finding words in a string using split can fail and hence requires regular expressions to perform this task. findall function returns the list after filtering the string and extracting words ignoring punctuation marks. The len() is used to compute string length.

Python3




# Python3 code to demonstrate Words lengths in String
# using regex( findall() method
 
import re
 
# initializing string
test_string = "Geeksforgeeks, is best @# Computer Science Portal.!!!"
 
# printing original string
print("The original string is : " + test_string)
 
# using regex( findall() )
# Words lengths in String
res = list(map(len, re.findall(r'\w+', test_string)))
 
# printing result
print("The list of words lengths is : " + str(res))


Output : 

The original string is : Geeksforgeeks is best Computer Science Portal
The list of words lengths is : [13, 2, 4, 8, 7, 6]

Time complexity: O(n), where n is the length of the test_list. The regex( findall() ) + len() takes O(n) time
Auxiliary Space: O(n), extra space of size n is required

Method #3: Using numpy

Note: Install numpy module using command “pip install numpy”

Python3




import numpy as np
 
# initializing string
test_string = "Geeksforgeeks is best Computer Science Portal"
 
# printing original string
print("The original string is : " + test_string)
 
# using numpy
res = np.char.str_len(test_string.split())
 
# printing result
print("The list of words lengths is : " + str(res.tolist()))
# This code is contributed by Edula Vinay Kumar Reddy


Output : 

The original string is : Geeksforgeeks is best Computer Science Portal
The list of words lengths is : [13, 2, 4, 8, 7, 6]

Another way to find the word length in a string is to use numpy. The numpy library has a function numpy.char.str_len() which can be used to find the length of each word in the string.

Time complexity: O(n)
Auxiliary Space: O(n)

Method #4: Using map()

In this approach, we first split the string into individual words using the split() method, which returns a list of words. We then use a generator expression to iterate over the words in the list and get their lengths using the len() function. The resulting generator object is passed as the second argument to the map() function, which applies the len() function to each element of the generator and returns an iterator of the resulting lengths.

Finally, we convert the iterator to a list using the list() function and return the resulting list of lengths.

In the example usage, we call the function with a sample string s. The output will be a list of integers representing the lengths of all the words in the string. In this case, the output will be [4, 2, 1, 6, 6], which corresponds to the lengths of the words “This”, “is”, “a”, “sample”, and “string”, respectively.

This approach using a generator expression and the map() function may be more memory-efficient than the previous approaches using list comprehensions, as it avoids creating a new list to store the lengths of the words. Instead, it produces the lengths as an iterator, which can be converted to a list if needed.

Python3




def word_lengths(s):
   
    # use a generator expression with map to get the lengths of all the words
    lengths = map(len, (word for word in s.split()))
    return list(lengths)
 
 
# input
s = "This is a sample string"
 
result = word_lengths(s)
 
print(result)  # output: [4, 2, 1, 6, 6]


Output

[4, 2, 1, 6, 6]

Time complexity: O(n)
Auxiliary Space: O(n)

Method 5: Using loop

Python3




# Python3 code to demonstrate
# Words lengths in String
# using loop
 
# initializing string
test_string = "Geeksforgeeks is best Computer Science Portal"
 
# printing original string
print("The original string is : " + test_string)
 
# Words lengths in String
# using loop
word_lengths = []
curr_word_length = 0
 
for char in test_string:
 
    if char == " ":
        word_lengths.append(curr_word_length)
        curr_word_length = 0
 
else:
    curr_word_length += 1
 
 # add last word length
word_lengths.append(curr_word_length)
 
# Printing word length in our list
print("The list of words lengths is : " + str(word_lengths))


Output

The original string is : Geeksforgeeks is best Computer Science Portal
The list of words lengths is : [13, 2, 4, 8, 7, 6]

Time complexity: O(n), where n is the length of the input string since we iterate over the string once.
Auxiliary space: O(m), where m is the number of words in the input string since we store the length of each word in a list.



Last Updated : 26 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads