Problem 5.4

Longest Argument

Write a program longest.py that takes a one or more words as command-line arguments and prints the longest word.

$ python longest.py joy of programming
programming

$ python longest.py this too shall pass
shall

Hint: You can use list slicing to get all the arguments. For example sys.argv[1:] will give you all arguments other than the program name.

Solution

import sys
args = sys.argv[1:]
print(max(args, key=len))