how to read input?

Andy Fox foxes at theriver.com
Tue Mar 21 13:08:17 EST 2000


The Man wrote:
> 
> what is the equivalent python code for the C code:
> printf("whats yer name");
> scanf("%s", name)
> printf("%s", name)
> =====================
> Shaun Hogan
> Interactive Enterprise Ltd.
> alt. E-mail : shaun_hogan at yahoo.com
> +353 86 8342529

Shaun,

Try this:

import sys
sys.stdout.write("enter your name:  ")	# doesn't add space or newline
response = sys.stdin.readline()		# this does the work
print "you typed:  %s" % response	# adds a newline


If you want to split up the input, try this:

import sys, string
divided = string.split(response)	# makes a list 
for x in divided:			# step through each one
    print x

print divided				# prints the whole list at once

Hope this helps,
Andy Fox



More information about the Python-list mailing list