137

What is the raw_input function? Is it a user interface? When do we use it?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Janezcka
  • 1,465
  • 3
  • 12
  • 12
  • 17
    Use `input()` instead. The function `raw_input` is no longer included in python. – Robino Apr 09 '18 at 15:43
  • 1
    Related post - [How do I use raw_input in Python 3](https://stackoverflow.com/q/954834/465053) – RBT Aug 03 '18 at 07:04

7 Answers7

155

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

TallChuck
  • 1,725
  • 11
  • 28
Andrea Spadaccini
  • 12,378
  • 5
  • 40
  • 54
115

raw_input() was renamed to input() in Python 3.

From http://docs.python.org/dev/py3k/whatsnew/3.0.html

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Chandragupta Borkotoky
  • 1,596
  • 1
  • 11
  • 16
6

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"

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
Nishant Kohli
  • 445
  • 6
  • 6
4

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'
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
Sreenivas
  • 95
  • 1
2

The raw_input() function reads a line from input (i.e. the user) and returns a string

Python v3.x as raw_input() was renamed to input()

PEP 3111: raw_input() was renamed to input(). That is, the new input() function reads a line from sys.stdin and returns it with the trailing newline stripped. It raises EOFError if the input is terminated prematurely. To get the old behavior of input(), use eval(input()).

Ref: Docs Python 3

Keiko Mori
  • 157
  • 2
  • 15
1

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)
Kapil Marwaha
  • 949
  • 9
  • 9
-1

If I let raw_input like that, no Josh or anything else. It's a variable,I think,but I don't understand her roll :-(

The raw_input function prompts you for input and returns that as a string. This certainly worked for me. You don't need idle. Just open a "DOS prompt" and run the program.

This is what it looked like for me:

C:\temp>type test.py
print "Halt!"
s = raw_input("Who Goes there? ")
print "You may pass,", s

C:\temp>python test.py
Halt!
Who Goes there? Magnus
You may pass, Magnus

I types my name and pressed [Enter] after the program had printed "Who Goes there?"

warvariuc
  • 57,116
  • 41
  • 173
  • 227
Loller
  • 9