raw_input function in Python
It presents a prompt to the user (the optional arg
of raw_input([arg])
), gets input from the user and returns the data input by the user in a string. See the docs for raw_input()
.
Example:
name = raw_input("What is your name? ")
print "Hello, %s." % name
This differs from input()
in that the latter tries to interpret the input given by the user; it is usually best to avoid input()
and to stick with raw_input()
and custom parsing/conversion code.
Note: This is for Python 2.x
raw_input()
was renamed to input()
in Python 3.
From http://docs.python.org/dev/py3k/whatsnew/3.0.html
raw_input
is a form of input that takes the argument in the form of a string whereas the input function takes the value depending upon your input.
Say, a=input(5)
returns a as an integer with value 5 whereas
a=raw_input(5)
returns a as a string of "5"
The "input" function converts the input you enter as if it were python code. "raw_input" doesn't convert the input and takes the input as it is given. Its advisable to use raw_input for everything. Usage:
>>a = raw_input()
>>5
>>a
>>'5'
Another example method, to mix the prompt using print, if you need to make your code simpler.
Format:-
x = raw_input () -- This will return the user input as a string
x= int(raw_input()) -- Gets the input number as a string from raw_input() and then converts it to an integer using int().
print '\nWhat\'s your name ?',
name = raw_input('--> ')
print '\nHow old are you, %s?' % name,
age = int(raw_input())
print '\nHow tall are you (in cms), %s?' % name,
height = int(raw_input())
print '\nHow much do you weigh (in kgs), %s?' % name,
weight = int(raw_input())
print '\nSo, %s is %d years old, %d cms tall and weighs %d kgs.\n' %(
name, age, height, weight)