+ 8

bug with split method in python

Please I need a help: when i write : ppl=str(input()).split(' ') and i enter for example 'Adam Bob Alan' i expect to get ['Adam','Bob','Alan'] but what i get actually is [" 'Adam ",'Bob'," Alan' "](without spaces) Ps: this only happens when ppl is an input Thanks for the help!

2nd Mar 2020, 8:47 PM
Othmane DAHI
Othmane DAHI - avatar
27 Respuestas
+ 10
So when a task tells you, input comes like this: 'a b c' ... they mean that they'll feed in just... a b c
2nd Mar 2020, 9:37 PM
HonFu
HonFu - avatar
+ 4
I have just copypasted your lines of code and entered input. Output exactly as you wanted! Not sure what the problem is... can you maybe recreate the *exact* situation that causes the bug? Btw, you don't need to use str - input is automatically string. Also, don't give split an argument, then it splits off all the whitespace, for example when you enter several spaces. (Maybe that's where your bug comes from.) So just: ppl = input().split()
2nd Mar 2020, 8:54 PM
HonFu
HonFu - avatar
+ 3
When you enter your input you don’t need the aphostrophe
2nd Mar 2020, 8:55 PM
Gabriel Ilie
Gabriel Ilie - avatar
2nd Mar 2020, 9:10 PM
Othmane DAHI
Othmane DAHI - avatar
+ 3
This is not a bug in python split method. If you write 'Adam Bob Alan' in the input window you will get the first element as 'Adam because ' is part of the text. You should try running the code with Adam Bob Alan.
2nd Mar 2020, 9:20 PM
Gabriel Ilie
Gabriel Ilie - avatar
+ 3
HonFu Thank you so much !!
2nd Mar 2020, 9:39 PM
Othmane DAHI
Othmane DAHI - avatar
+ 3
Simple just try typing Ppl=str(input ()). split () In this manner the out put will be as follows; Ppl=sylencer dranoel Frank Output: ['sylencer', 'dranoel', 'frank'] the code itself will produce an output that will separate the items with commas . because you asked it to split () them
3rd Mar 2020, 12:00 PM
Sylencer
+ 3
Input is string by default. Isn't necessary to use str(). Remove it and that will fix ppl=input ().split (' ') Also you can write this ppl =input (). split ()
4th Mar 2020, 7:56 PM
Alireza Aliabadi
Alireza Aliabadi - avatar
+ 1
Gabriel Ilie but when i write 'Adam Bob Alan'.split() it returns ['Adam','Bob','Alan'] why please ?
2nd Mar 2020, 9:22 PM
Othmane DAHI
Othmane DAHI - avatar
+ 1
Did it. Output as expected: 3 words in a list.
2nd Mar 2020, 9:24 PM
HonFu
HonFu - avatar
+ 1
When you write it like this the ‘ is not part of the text. What is inside them is the text.
2nd Mar 2020, 9:25 PM
Gabriel Ilie
Gabriel Ilie - avatar
+ 1
are u running it on sololearn please or on another compiler
2nd Mar 2020, 9:25 PM
Othmane DAHI
Othmane DAHI - avatar
+ 1
Gabriel Ilie Okay thanks My only problem left is how to make a list with the input ?
2nd Mar 2020, 9:27 PM
Othmane DAHI
Othmane DAHI - avatar
+ 1
Wait, maybe I got you. Are you actually inputting this... 'bla bla' ... instead of this? bla bla Quotation marks are just... well, *marks*, to show where your string starts and where it ends, so that Python can distinguish it from the rest of the code. If you write... x = 'Hello!' ... the actual string is just... Hello! (This is also true in real life btw. Like when you read a book, and people talk.) So just input without quotation marks - problem solved!
2nd Mar 2020, 9:30 PM
HonFu
HonFu - avatar
+ 1
HonFu You were entering the input like that : Adam Bob Alan i mean without ' right ?
2nd Mar 2020, 9:31 PM
Othmane DAHI
Othmane DAHI - avatar
+ 1
HonFu my problem is the input is fixed as 'Adam Bob Alan' including ' and i have to add them to a list
2nd Mar 2020, 9:32 PM
Othmane DAHI
Othmane DAHI - avatar
+ 1
HonFu It's the Driver's new licence problem in sololearn
2nd Mar 2020, 9:33 PM
Othmane DAHI
Othmane DAHI - avatar
+ 1
Yeah. Which is kind of obvious, isn't it? When you write your e-mail address into your provider's input bar, do you write... name@provider.ending ... or... 'name@provider.ending' ?
2nd Mar 2020, 9:33 PM
HonFu
HonFu - avatar
+ 1
HonFu the first one ofc
2nd Mar 2020, 9:35 PM
Othmane DAHI
Othmane DAHI - avatar
+ 1
The description in the problem also just marks the words with ' so that YOU know that this is what it comes as input. When you have to enter some' I am no bot code' at some homepage, and they ask you: Please type 'rj843f'! Would you type 'rj843f' or just rj843f?
2nd Mar 2020, 9:36 PM
HonFu
HonFu - avatar