Newbie Question...

Gregory Jorgensen gregj at pobox.com
Sun Feb 25 20:23:22 CET 2001


In article <kscm6.25549$MN.622090 at news2-win.server.ntlworld.com>, Michael
Ellwood says...
>
>I've been looking, but I can't find any reference in the Python docs of how
>to get python to prompt the user to input a string - can someone please help
>me with this (im writing a program to crack some ciphers from a book...)

If you are using IDLE or running scripts from a command line, you just read from
standard input (the file descriptor sys.stdin). This file is opened for you.

# read a line from standard input
import sys
s = sys.stdin.readline()

You can prompt the user with print:

print "enter a string:"
s = sys.stdin.readline()

If you want the prompt and the input on the same line, write to stdout instead
of using print:

sys.stdout.write("enter a string: ")
s = sys.stdin.readline()
print "you entered: %s" % s

The built-in function raw_input(prompt) is a shortcut to the last example:

s = raw_input("enter a string: ")
print "you entered: %s" % s

If you are writing a GUI application (with TKinter, for example) you'll have to
use the widgets and event model of your GUI toolkit.

I recommend the very useful book "Python Essential Reference" by David Beazley.


Greg Jorgensen
Deschooling Society
Portland, Oregon, USA
gregj at pobox.com



More information about the Python-list mailing list