What is the Difference between raw_input and input? I can't use them interchangeably in my code. For example, x=raw_input("enter your date of birth") print x ## x is the first question y=raw_input("enter your last name") print y z=x + y print z if I use input instead of raw_input, it gives me error. What is this?
The main difference is that, while raw_input creates a string object, input actually evaluates (executes) the input received, i.e., it WILL run an input command such as 'rm -r *'. Not something you would like to happen. Short answer, never use input(). It's way too unsafe. Long answer, the difference is hard to fully understand, at least at a beginning level. Check Python's docs online, and try to understand it. If you feel you need more explanation, don't be afraid to ask again. P.S.: My answer is for Python 2.x. Input in Python 3.x was changed.
do you know what about input in Python 3.x was changed?
Yes. input() replaced raw_input. http://docs.python.org/release/3.1.3/library/functions.html#input The key thing is "The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that." Compare to 2.x input: "Equivalent to eval(raw_input(prompt)). This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation." (found here: http://docs.python.org/library/functions.html#input ) and to raw_input: "The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that." :-) Sorry for the badly formatted answer. Hopefully it was reasonably clear. Anyway, ask again if it wasn't :-)
Join our real-time social learning platform and learn together with your friends!