954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to do Input in Python?

I've skimmed the manual and cant find the answer to this anywhere:

In basic, to add an Input function you would do (for example):

Input "What is your name? " name$


How do I do this in Python?

Cup of Squirrel
Junior Poster
133 posts since Oct 2004
Reputation Points: 11
Solved Threads: 1
 

According to my trusty Python Pocket Reference, it's the input() function:

input([Prompt])

Where [ Prompt ] is the string you would want to prompt the user with, obviously. So, from what I can tell:

foo=input('Please enter a value:')


I'm rusty, but I think that would printPlease enter a value:, and assign the user input to the variable foo. Or, some combination of that should work. Like I said, I'm kind of rusty, but plan on jumping back in to Python Real Soon Now...

alc6379
Cookie... That's it
Team Colleague
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
 

the correct way is

var = raw_input("Enter something: ")
print "you entered ", var


using input is unsafe as it evaluates your python code as commands and can cause someone to hack your code.

DrLight
Newbie Poster
2 posts since Nov 2004
Reputation Points: 14
Solved Threads: 1
 

the correct way is

var = raw_input("Enter something: ")
print "you entered ", var

using input is unsafe as it evaluates your python code as commands and can cause someone to hack your code.

I didn't know that! You've at least enlightened two people.

Just out of curiosity, what does raw_input() do different from input()? Does it escape out everything, like !'s, /'s, and such? Or, do something to where they couldn't define/change other variables with their input? Am I close?

alc6379
Cookie... That's it
Team Colleague
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
 

I didn't know that! You've at least enlightened two people.

Just out of curiosity, what does raw_input() do different from input()? Does it escape out everything, like !'s, /'s, and such? Or, do something to where they couldn't define/change other variables with their input? Am I close?


raw_input accepts your input as a string. input accepts it as a command. so for example lets say you had

input("somehthing")

and a hacker knowing some exploit in your code or system used the python function

eval


which basically evaluates a string as a command. you wouldn't want that. I had some code lying around here somewhere when i was testing it out, i'll try and find it for you.
For now just remeber to use only raw_input, if its imperitive to have a num then simply

int(input)


should do the trick.

DrLight
Newbie Poster
2 posts since Nov 2004
Reputation Points: 14
Solved Threads: 1
 

I've skimmed the manual and cant find the answer to this anywhere:

In basic, to add an Input function you would do (for example):

Input "What is your name? " name$

How do I do this in Python?

From what I understand you would use, input as to prompt the user to input a number and raw_input to prompt a string

reezin14
Junior Poster
107 posts since Jan 2005
Reputation Points: 12
Solved Threads: 4
 

The correct answer is from
http://www.daniweb.com/techtalkforums/thread20774.html

# 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
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

I have another problem with pythons Input, and I was wondering if someone could help me. Whenever I get any input from the user, python keeps printing out on a new line.
So for example:

test = raw_input ("Input word")
print "Why "+ test +" there a line break all the time?"


So if my inputted word was "is", the program would output this:

Why is
there a line break all the time?


Im guessing its because python is including the ENTER key used whenever I'm finished with input, but I'm not sure how to stop that. Any help would be appreciated. :D

Rete
Newbie Poster
23 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 

Just a moment ...

I can't repeat your problem, both PythonWin and IDLE give a perfect one line result!

I tried to mimic your predicament ...

test = raw_input ("Input word")
print "Why "+ test +" there a line break all the time?"

# add a newline to the end for testing
test = test + '\n'
print "Why "+ test +" there a line break all the time?"

# simple way to remove a trailing newline
if '\n' in test:
    test = test[:-1]
print "Why "+ test +" there a line break all the time?"
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

I have another problem with pythons Input, and I was wondering if someone could help me. Whenever I get any input from the user, python keeps printing out on a new line. So for example:

test = raw_input ("Input word") print "Why "+ test +" there a line break all the time?"

So if my inputted word was "is", the program would output this:

Why is there a line break all the time?

Im guessing its because python is including the ENTER key used whenever I'm finished with input, but I'm not sure how to stop that. Any help would be appreciated. :D


Looks like a funny issue, I couldn't re - produce it. But Ican suggest you the solution.

try this,

print "Why "+ test.strip() +" there a line break all the time?"

It has to work out, strip will help you to remove the newline charecter from test

tharippala
Newbie Poster
2 posts since Oct 2007
Reputation Points: 10
Solved Threads: 1
 

print 'What is you name?'
Raw_input()

Flare
Newbie Poster
1 post since Dec 2008
Reputation Points: 10
Solved Threads: 1
 

Or you can one-line it:
print raw_input('What is your name? ')

mn_kthompson
Junior Poster
148 posts since Nov 2007
Reputation Points: 16
Solved Threads: 35
 
print 'What is you name?' Raw_input()

If you run it you will get error
NameError: name 'Raw_input' is not defined

That is because your version of raw_input() is Raw_input()

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 
From what I understand you would use, input as to prompt the user to input a number and raw_input to prompt a string

No. You would use raw_input() for both normally, but you add int() if what you want is an int, or float() if you want a floating point number.

s = raw_input( "Type a string: " ).rstrip( '\n' )
n = int( raw_input( "Type an int: " ).rstrip( '\n' ) )
f = float( raw_input( "Type a float: " ).rstrip( '\n' ) )


The "rstrip('\n')" takes the newline character off the end of what they type.

The only time you would use input() is if you want the user to be able to type any Python code they want, so they have the power to type in a complicated expression like "sin( pi/7 )" ... or erase all your files ... as part of their answer.

mac-guyver
Newbie Poster
4 posts since May 2009
Reputation Points: 10
Solved Threads: 1
 

Looks like a funny issue, I couldn't re - produce it. But Ican suggest you the solution.

try this,

print "Why "+ test.strip() +" there a line break all the time?"

It has to work out, strip will help you to remove the newline charecter from test

Well, strip() will take any whitespace characters off the beginning or end of the string. If you really only want to take the newline off the end, do this:

print "Now " + test.rstrip( '\n' ) + " doesn't have a break in it."
mac-guyver
Newbie Poster
4 posts since May 2009
Reputation Points: 10
Solved Threads: 1
 

stdout vs. stderr

One more issue with input() and raw_input() is that they print the prompt on the standard output! In Unix I almost always want prompts to go to standard error instead. But doing this is tricky. If you do this:

from sys import stderr
print >>stderr, "Type a string: ",
s = raw_input().rstrip( '\n' )
print >>stderr, "You typed:", s

Then you get something like this:

Type a string: hello
 You typed: hello

An extra space comes out on the next print to stderr because of the comma when you printed the prompt. The only correct way to do it I know is this:

from sys import stderr
stderr.write( "Type a string: " )
stderr.flush()
s = raw_input().rstrip( '\n' )
print >>stderr, "You typed: ", s


(Hey, emacs control characters work in this editor, kewl!)

mac-guyver
Newbie Poster
4 posts since May 2009
Reputation Points: 10
Solved Threads: 1
 

Forgot, I wanted to say: rstrip( '\n' ) is what you typically want to do to lines you read from a file. It won't take indentation off the lines! E.g.,

for line in open( filename ):
   line = line.rstrip( '\n' )
   # blah blah...
mac-guyver
Newbie Poster
4 posts since May 2009
Reputation Points: 10
Solved Threads: 1
 

I have another problem with pythons Input, and I was wondering if someone could help me. Whenever I get any input from the user, python keeps printing out on a new line. So for example:

test = raw_input ("Input word") print "Why "+ test +" there a line break all the time?"

So if my inputted word was "is", the program would output this:

Why is there a line break all the time?

Im guessing its because python is including the ENTER key used whenever I'm finished with input, but I'm not sure how to stop that. Any help would be appreciated. :D

There's a faster fix to this, just use this:

test = raw_input ("Input word").strip()
print "Why "+ test +" there a line break all the time?"


That beats having to make sure you enter the ".strip()" every time that you want to print the var like this:

test = raw_input("Input word")
print "Why" + test.strip() + " there a line break all the time?"


You could also make it easier by doing this:

test = raw_input("Input word").strip()
print "Why %s there a line break all the time?" % test


Or to use the first example:

var = raw_input("Type something: ").strip()
print "You typed %s" % var


Or, maybe you have multiple vars:

name = raw_input("Enter your name: ").strip()
email = raw_input("Enter your email: ").strip()
print "Hello %s!\nThe email address we have for you is '%s'." % (name, email)


Or, better yet, make the prompt a function if you're asking for a lot of information:

import sys
def prompt():
    response = sys.stdin.readline().strip()
    return response

fields = [ "Name: ", "Email: ", "Phone: " ]

answers = []
for field in fields:
    print field,
    v = prompt()
    answers += [v]

print """Hello %s!
Your Email is: %s
Your Phone is: %s""" % ( answers[0], answers[1], answers[2] )


With the above process, the user will see:Name: Joe
Email: [email]joe@domain.com[/email]
Phone: 555-1234

Hi Joe!
Your Email is: [email]joe@domain.com[/email]
Your Phone is: 555-1234

Hope that helps!

Hummdis
Light Poster
48 posts since Jun 2009
Reputation Points: 15
Solved Threads: 5
 

how to save in the memory of the phone with rms + python

aksegaly
Newbie Poster
4 posts since Oct 2009
Reputation Points: 10
Solved Threads: 1
 
how to save in the memory of the phone with rms + python

Vary bad manners to hijack this thread for such question.

Start your own thread and give more info!

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You