Go to: Na-Rae Han's home page  

Python 2.7 Tutorial

With Videos by mybringback.com

#6: Interactive Script with raw_input()

                          << Previous Tutorial           Next Tutorial >>
On this page: raw_input(), print, + and * on string

Get Started

Video Summary

  • Three versions of the script is used in the tutorial: Version 1, Version 2, and Version 3. The interactive shell output of the scripts being executed.
  • Functions can be used to allow for interactivity between your program and the user. The function raw_input() displays a displays a desired string to the user and asks for an input from them, while the function "print" displays a response.
  • In this tutorial, a = raw_input('please type a word: ') is used to display the message please type a word: to the user. (See 2nd version of the script.) Upon the user entering a response and hitting the enter key, the subsequent function print (a+' ')*5 takes the string entered by the user, multiplies it by five, and gives that as a response. As seen in the previous tutorial, multiplication is carried out on a non-numeric string by printing it in a row the number it is being multiplied by: in this case, 5.
  • Remember order of operations when using Python. print a+' '*5 will provide a different answer than print (a+' ')*5 because multiplication comes earlier within the order of operations.
  • Remember that Python will close a given program once all functions are completed within it.

Learn More

  • Note that the user input is always the string type, even when you enter a number. If you enter, say, '16', the result will be a string '16' rather than an integer 16. You can convert the string to an integer by using the int() function.

Explore