+ 11
That python code made me headaches
15 Réponses
+ 10
@Oma you just missed a (, )(separator) in list just place (,) you will get your output
sentence = [
'This',
'is ',
'a' ,'sentence'
]
+ 8
Don't know, but I played around with it and it seems you can interchange single for double quotes, as well as docstrings and they all work fine like this. You won't be able to accomodate str() there, however. I assume it is some kind of a shortcut for string concatenation, but works only for implicit strings, not for convertites ;)
+ 7
@ Gawen ... it should be an error
+ 7
Cool! 😁
sentence = [
'a' ' ' 's' 'e' 'n' 't' 'e' 'n' 'c' 'e'
]
print(sentence)
>>>
['a sentence'] # len == 1
+ 7
@Kuba @Gawen Okay but i am still not happy with it. Why did Guido do THAT????
what does this lout think about how much time I have to become aware of this ???
+ 7
@Kuba okay this is a nice application for it.
a clear case for submitting a challange isnt it?
+ 7
@Kuba sir explained well it seems like python interpreter understand the gap between two string by only by seperater (,) unless it is not interprete the space like
print('red' 'yellow')
output=> redyellow
if I am wrong please correct me @Kuba sir @oma😊😊
+ 6
no it will not generate any error because word is separated by (,) so between (,) it will treat it like a word for checking that do like this
print(sentence[-1])
last element is output by this which is output as
output->asentence
+ 5
@Oma If a quiz question, that's gonna be a nasty one ;)
Honestly saying, I can't think of any particular reason why it is like so, but perhaps this is more convenient in some cases.
Or maybe it is its intrinsic Pythonicness which simplifies everything -- since there is no particular difference between those two versions, let's assume the easier one :)
+ 5
@Gordie Nice find. Good to know! 👍
Seems like the best answer here... 😏
+ 5
thanks for all your answers!
There are some nice treasures in python syntax!
+ 1
place , between last and second last element
0
you have forgotten to add a comma between 'a' and 'sentence' so the compiler says the length is 3 instead of 4 as it reads that the array is composed of 3 elements instead of 4
0
The last two strings that don't have a separator are taken as one string. When I tried printing the list, the last element was 'asentence'.