How do I read an input seperated by spaces in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I read an input seperated by spaces in python

Example: a user is to enter their favourite food and drink (seperated by spaces) and the program should read and display the food and drink

19th Aug 2023, 12:05 AM
Adrian Maphosa
Adrian Maphosa - avatar
4 Answers
+ 1
You need to split the input into a list. Here is an example: https://code.sololearn.com/cD3yGMBN4nc3/?ref=app
19th Aug 2023, 12:58 AM
Keith
Keith - avatar
+ 3
The input() function already reads whole lines, with spaces or not. If what you want is separate out each, you can also call the split method on the string created from the input and create a list of the elements of the string according to a delimiter, in this case, a whitespace.
19th Aug 2023, 12:08 AM
Marcos Chamosa Rodríguez
Marcos Chamosa Rodríguez - avatar
+ 2
and for spaces use this: input().split(' ')
19th Aug 2023, 10:48 AM
Angela
Angela - avatar
+ 2
I updated my code! I somehow misread 'spaces' for 'commas' LOL 😉
19th Aug 2023, 11:40 AM
Keith
Keith - avatar