-1

I want to write a code that takes a few words from the user and puts the words in the list. How can I do this? for example: user write ( red blue gray black) and code display [red,blue,gray,black]

Y.Z
  • 9
  • 1
  • [`str.split()`](https://docs.python.org/3/library/stdtypes.html#str.split) is what you are looking for. – Ch3steR Apr 07 '20 at 05:44

1 Answers1

1

Solution in one line:

print('[%s]' % ','.join(input().split()))

How it works:

  • the input takes the input from user, returning it as string
  • string split method separates the string into multiple single word strings
  • the string join method, called on ',', joins the separate strings back with ',' between them
  • Finally, the print function prints the string with [ and ] around it
Boris Lipschitz
  • 1,514
  • 8
  • 12
  • Note that if you just want to put words in the list, and print the list (in your example, this would be `['red', 'blue', 'gray', 'black']`) you can do just `print(input().split())`, I've added the rest just to match your exact output. – Boris Lipschitz Apr 07 '20 at 05:58