05 January 2011

Python raw_input() vs input()

# raw_input() reads every input as a string
# then it's up to you to process the string

str1 = raw_input("Enter anything:")
print "raw_input =", str1

# input() actually uses raw_input() and then tries to
# convert the input data to a number using eval()
# hence you could enter a math expression
# gives an error if input is not numeric eg. $34.95

x = input("Enter a number:")
print "input =", x


The code example above is from this link. One other thing to note here is that there is no raw_input in Python 3 anymore, only input() function available.

No comments: