views:

591

answers:

4

I have this code in Python

inputted = input("Enter in something: ")
print("Input is {0}, including the return".format(inputted))

that outputs

Enter in something: something
Input is something
, including the return

I am not sure what is happening; if I use variables that don't depend on user input, I do not get the newline after formatting with the variable. I suspect Python might be taking in the newline as input when I hit return.

How can I make it so that the input does not include any newlines so that I may compare it to other strings/characters? (e.g. something == 'a')

+5  A: 

You are correct - a newline is included in inputted. To remove it, you can just call strip("\r\n") to remove the newline from the end:

print("Input is {0}, including the return".format(inputted.strip("\r\n")))

This won't cause any issues if inputted does not have a newline at the end, but will remove any that are there, so you can use this whether inputted is user input or not.

If you don't want any newlines in the text at all, you can use inputted.replace("\r\n", "") to remove all newlines.

Daniel G
Thanks! Unfortunately, neither strip nor replace worked for me, on a separate line or otherwise.
wrongusername
Then try stripping '\r' instead of '\n'. Your terminal probably sends '\r\n' when the script expects '\n' alone.
Jacek Konieczny
Ah, thanks Jacek! That worked too! :)
wrongusername
Interesting - Python normally removes all newlines with just `"\n"`, or at least I thought it did (http://www.python.org/dev/peps/pep-0278/). I'll edit to strip \r\n, which should do both.
Daniel G
A nit: rstrip() is preferred to strip() here: strip() removes from both ends. It's true that you know there are no extra newlines at the start, but why not say what you mean?
Darius Bacon
+2  A: 
inputted = inputted.strip()

Edit: As noted, this will kill all whitespace at the start and end. A way to get rid of only the trailing newline is:

import re
inputted = re.sub("[\n\r]+$", "", inputted)
calmh
This will strip all whitespace - including line-ends, but not also spaces and tabs. Maybe precisely the right thing to do - but maybe not.
Steve314
@Steve True. Edited to reflect this.
calmh
@calmh - false, actually - how did that first "not" get in there? Damn!
Steve314
Actually, calmh's method worked too.
wrongusername
@Steve I mentally corrected the "not". ;)
calmh
+2  A: 

If you only want to stript the last line endings, you could use rstrip.
inputted.rstrip ("\r\n")

evilpie
+4  A: 

Your problem is actually Eclipse. Assuming that you use PyDev, I was able to reproduce the problem. When entering something in the Eclipse console, the problem occurs as described in your question. But when directly executing the very same script with the Python 3.1.1 interpreter, inputted does not include a newline character.

I investigated the Python source code and found out input() uses GNU readline if stdin is interactive (i.e. a TTY or prompt, however you want to call it), but falls back to the .readline() method of the stdin object if necessary. Then, if the result of readline ends with \n, that character is removed. Note: No CR-LF or LF-CR handling here (in the fallback case)!

So I wrote this little script to see what actually happens:

import sys
from io import StringIO

for stdin in [sys.stdin, StringIO("test\r\ntest\r\n")]:
    sys.stdin = stdin 

    print("readline returns this: " + repr(sys.stdin.readline()))

    inputted = input("Enter in something: ")
    print("inputted: " + repr(inputted))

    print("inputted is printed like this: --> {0} <--".format(inputted))

It first executes the code with the normal stdin (console or Eclipse console) and then with a prepared stdin containing the text test\r\ntest\r\n.

Try and run the script in Eclipse - you must enter a string twice. The conclusion: Pressing Enter in the Eclipse console will produce CR-LF ("\r\n"). Printing "\r" in the Eclipse console will jump to the next line.

On the other side, running it in the Windows console will produce the expected output: input() returns a string without a newline at the end because (I guess) GNU readline is used. With the prepared stdin StringIO("test\r\n"), the input() result is "test\r" as in Eclipse (although not printed as newline).

Hope this all makes sense... but what I still don't know is if that is expected behavior of Eclipse.

AndiDog
Darn Eclipse :) I guess I'll switch back to Netbeans, but I couldn't get Python 3 unittests working correctly in Netbeans (only managed to run Jython 2.5 unittests). Thanks for your answer!
wrongusername