Python printing array lists and length using definine function -
i trying print list in variable "words" , print length of each word. know how print when don't use def function not sure how print using below method.
words = ["good", "joy", "computer"] def list1(words): x in words: print x return x, len(x) print list1(words) this giving me:
good joy computer ('computer', 8) what can prints this.
good 4 joy 3 computer 8 thanks
you need make mind whether function should print or return string print. here example of latter:
words = ["good", "joy", "computer"] def list1(words): lines = ('{} {}'.format(x, len(x)) x in words) return '\n'.join(lines) print(list1(words)) if one-liners that:
print('\n'.join('{} {}'.format(x, len(x)) x in words))
Comments
Post a Comment