+ 5

In the Code Coach Challenges the length of the input seems to be longer than the actual input.

When i read the input in python using something like: Word = input() or words = input().split() The string named word or the last element in words is 1 longer than the actual word. This does not seem to be the case in the code playground. Does anyone know why this is and how to avoid this?

24th Dec 2019, 8:10 AM
davy hermans
davy hermans - avatar
19 ответов
+ 8
You can use input().strip(), that removes any invisible whitespace like \n or \r at the edges and doesn't do harm if there wasn't any.
24th Dec 2019, 10:20 AM
HonFu
HonFu - avatar
+ 3
davy hermans yes it is the return carriage /r at the end of some strings. You can check this with a print of the string as your first output in Code Coach chalenge cases 1 and 2.
24th Dec 2019, 8:20 AM
Mihai Apostol
Mihai Apostol - avatar
+ 2
davy hermans I guess in Code Coach the input is automated hence the /r problem. I got the same issue.
24th Dec 2019, 9:26 AM
Mihai Apostol
Mihai Apostol - avatar
+ 2
HonFu Just to wrap it up. You were right. I've confused strip with split from your first post. Second your code bit from your other post I was expecting to return chars one by one because I was inputing a string of only one word no space that is why I said it is not working. davy hermans sorry for highjacking your post. HonFu constructive discution, at least from my side, hope I didn't offend you in any way.
24th Dec 2019, 5:28 PM
Mihai Apostol
Mihai Apostol - avatar
+ 1
Mihai Apostol thanks for your answer. I saw the /r but i wonder why i don't see this in the code playgound. Is it because of the way the input is presented?
24th Dec 2019, 9:05 AM
davy hermans
davy hermans - avatar
+ 1
HonFu thanks, that is exactly what i was looking for.
24th Dec 2019, 11:27 AM
davy hermans
davy hermans - avatar
+ 1
HonFu thou...check this two codes: x = input() for char in x: print(char) x = input().split() for char in x: print(char)
24th Dec 2019, 2:52 PM
Mihai Apostol
Mihai Apostol - avatar
+ 1
Mihai Apostol if i am right, the first one is a string, the second a list.
24th Dec 2019, 3:36 PM
davy hermans
davy hermans - avatar
+ 1
davy hermans it is the other way around. With the first one I was able to go through the chars one by one with the second I cannot.
24th Dec 2019, 3:42 PM
Mihai Apostol
Mihai Apostol - avatar
+ 1
HonFu just ran it in playground...not working...
24th Dec 2019, 3:50 PM
Mihai Apostol
Mihai Apostol - avatar
+ 1
HonFu a string
24th Dec 2019, 3:51 PM
Mihai Apostol
Mihai Apostol - avatar
+ 1
HonFu I know what it doesn't do. It is not going through the chars one by one.
24th Dec 2019, 3:57 PM
Mihai Apostol
Mihai Apostol - avatar
+ 1
You said: "You can use input().strip(), that removes any invisible whitespace like \n or \r at the edges and doesn't do harm if there wasn't any." I said: "HonFu thou...check this two codes: x = input() for char in x: print(char) x = input().split() for char in x: print(char)" You said that using split doesn't do any harm, I just said I cannot go through the string one char at a time. It is on topic, as I was interesed to remove those \r at the end of some strings in code coach.
24th Dec 2019, 4:23 PM
Mihai Apostol
Mihai Apostol - avatar
+ 1
strip - not split - is what I said to be not doing any harm. Split returns a list of strings, and strip only works with strings. So you have to use strip on each word in the list - which my code example does. What exactly is 'not working' with it? (I tried it - does just what I said.)
24th Dec 2019, 4:26 PM
HonFu
HonFu - avatar
+ 1
Hum..
25th Dec 2019, 9:46 PM
Max Andy MOUMOUNI
Max Andy MOUMOUNI - avatar
0
Mihai Apostol, x = input().split() for word in x: print(word.strip())
24th Dec 2019, 3:44 PM
HonFu
HonFu - avatar
0
Mihai Apostol What did you input?
24th Dec 2019, 3:51 PM
HonFu
HonFu - avatar
0
Do you understand what that piece of code is supposed to do?
24th Dec 2019, 3:52 PM
HonFu
HonFu - avatar
0
It's not the point of these lines to do that, so it would be strange if they did. What exactly do you want to know? And has it a relation to this question? (If not, you should make a post of your own.)
24th Dec 2019, 4:14 PM
HonFu
HonFu - avatar