< BACKMake Note | BookmarkCONTINUE >
156135250194107072078175030179198180024228156016206217188240240204173118092008038004069168

Program Input and the raw_input() Built-in Function

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.

					
>>> user = raw_input('Enter login name: ')
Enter login name: root
>>> print 'Your login is:', user
Your login is: root

				

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

					
>>> num = raw_input('Now enter a number: ')
Now enter a number: 1024
>>> print 'Doubling your number: %d' % (int(num) * 2)
Doubling your number: 2048

				

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. See Section 6.5.3 for more information in the raw_input() built-in function.


Last updated on 9/14/2001
Core Python Programming, © 2002 Prentice Hall PTR

< BACKMake Note | BookmarkCONTINUE >

© 2002, O'Reilly & Associates, Inc.