raw_input(), Input in Python

The easiest way to obtain user input from the command-line is with the raw_input() built-in function. It reads from standard input and assigns the string value to the variable you designate. You can use the int() built-in function (Python versions older than 1.5 will have to use the string.atoi() function) to convert any numeric input string to an integer representation.

>>> name = raw_input("Enter your name");
Enter your name nash
>>> print 'your name is ',name
your name is   nash

The above example was strictly for text input. A numeric string input (with conversion to a real integer) example follows below:

>>> number1 = raw_input('enter a number ')
enter a number 123
>>> print 'number +1 equals %d'%(int(number1)+1)
number +1 equals 124

The int() function converts the string num to an integer, which is the reason why we need to use the %d (indicates integer) with the string format operator.


You may also like...