python Write a function pack_to_5(words) that takes a list of string objects as a parameter and returns a...

50.1K

Verified Solution

Question

Programming

python

Write a function pack_to_5(words) that takes a list ofstring objects as a parameter and returnsa new list containing each stringin the title-case version. Any strings that haveless than 5 characters needs tobe expanded with the appropriate number of space characters to makethem exactly 5 characters long. For example,consider the following list:

words = ['Right', 'SAID', 'jO']

The new list would be:

['Right', 'Said ', 'Jo  ']

Since the second element only contains 4 characters, an extraspace must be added to the end. The third element only contains 2characters, so 3 more spaces must be added.

Note:

  • Title case means that the first letter of each word isuppercase and the rest lower-case. You may want to use the .title()string function. (e.g. 'SMITH'.title() returns 'Smith')
  • You should not modify the original list of string objects.
  • You may assume that each word in the list does not have morethan 5 characters.
  • You may want to use the format string: '{:<5}' in thisquestion.

For example:

TestResult
words = ['Right', 'SAID', 'jO']result = pack_to_5(words)print(words)print(result)
['Right', 'SAID', 'jO']['Right', 'Said ', 'Jo  ']
words = ['help', 'SAID', 'jO', 'FUNNY']result = pack_to_5(words)print(words)print(result)
['help', 'SAID', 'jO', 'FUNNY']['Help ', 'Said ', 'Jo  ', 'Funny']
result = pack_to_5([])print(result)
[]

Answer & Explanation Solved by verified expert
3.6 Ratings (295 Votes)
Codedef packto5 words packto5 functionfinalresult finalresult listfor word in wordtitle for word in words for each word inwords convert it to title case and take every word in thatif lenword5 check if the word lenght is 5 or not if    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students