Write a function, lenWords(STRING), that takes a string as an argumentand returns a tuple containing length of each word of a string.For example, if the string is "Come let us have some fun", thetuple will have (4, 3, 2, 4, 4, 3)
Question
Write a function, lenWords(STRING), that takes a string as an argumentand returns a tuple containing length of each word of a string.For example, if the string is "Come let us have some fun", thetuple will have (4, 3, 2, 4, 4, 3)
Solution
Sure, here is a Python function that does what you asked for:
def lenWords(STRING):
words = STRING.split() # split the string into words
lengths = tuple(len(word) for word in words) # create a tuple with the length of each word
return lengths
You can use this function like this:
print(lenWords("Come let us have some fun")) # prints (4, 3, 2, 4, 4, 3)
Similar Questions
The function LEN() returns the total number of characters in a string
Write a program to count number of words in a given string whose length is greater than 3.For example:Input Resultpython is easier than C3
Write a Python function that takes a list of words and return the longest word and the length of the longest one.
Your task is to design a string calculator with two functions:Create a class with 2 overloaded member functions.The function has to return the concatenated string if it receives 4 characters as an argument.The function has to return the list of words if it receives a single string argument.
// write a method to find length of string and write 3 test cases
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.