+ 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!
27 Réponses
+ 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
+ 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()
+ 3
When you enter your input you don’t need the aphostrophe
+ 3
+ 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.
+ 3
HonFu
Thank you so much !!
+ 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
+ 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 ()
+ 1
Gabriel Ilie
but when i write 'Adam Bob Alan'.split() it returns ['Adam','Bob','Alan']
why please ?
+ 1
Did it.
Output as expected:
3 words in a list.
+ 1
When you write it like this the ‘ is not part of the text. What is inside them is the text.
+ 1
are u running it on sololearn please or on another compiler
+ 1
Gabriel Ilie
Okay thanks
My only problem left is how to make a list with the input ?
+ 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!
+ 1
HonFu
You were entering the input like that : Adam Bob Alan
i mean without '
right ?
+ 1
HonFu
my problem is the input is fixed as 'Adam Bob Alan' including ' and i have to add them to a list
+ 1
HonFu
It's the Driver's new licence problem in sololearn
+ 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'
?
+ 1
HonFu
the first one ofc
+ 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?