Python
  Home arrow Python arrow Page 2 - Python: Input and Variables
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python: Input and Variables
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2007-11-19

    Table of Contents:
  • Python: Input and Variables
  • Beating...I Mean Asking the User for Information
  • Loops
  • For Loop

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Python: Input and Variables - Beating...I Mean Asking the User for Information


    (Page 2 of 4 )

    So now that we know everything there is to know about printing and we know about variables, let's learn how to get data from the user and work with it.


    #!/usr/local/bin/python


    print “Go no further!”

    name = raw_input(“Who are you?”)

    print “Did you say:”, name

    The above code prints a statement: Go no further! Then it asks the user: Who are you? and prompts them for some text. When the user types in the text and presses enter, it then prints: Did you say: and appends the value of the variable name to the end.

    Let's say the user entered in the name: The Knights Who Go Neep! This is what the code would print:

      Go no further!

      Who are you?

      Did you say: The Knights Who Go Neep!

    We use raw_input when we want to get textual input from a user. If we wanted to get a numeric value, we would use input, like so:


    #!/usr/local/bin/python


    age = input(“Enter your age: “)

    print “You are”, age

    print “If I were twice your age I would be: “, age * 2

    Same principle as before. If the user had entered an age of 20, this would be the printout result:

      Enter your age:

      You are 20

      If I were twice your age I would be 40

    And of course we could always combine the two together:


    #!/usr/local/bin/python


    first_name = raw_input(“What is your first name?”)

    last_name = raw_input(“What is your last name?”)

    age = input(“How old are you?”)


    print “Your full name is “, first_name + last_name

    Print “And this is your age: “, age

    If the user had answered Lando Calrissian 50, then the printout would be:

      Your full name is LandoCalrissian

      And this is your age: 50

    When you use the + symbol with text it is called concatenating strings, or adding them to one another. You will note that the values in the concatenated sentence have no space. You must add one to the sentence like so:


    #!/usr/local/bin/python


    first_name = raw_input(“What is your first name?”)

    last_name = raw_input(“What is your last name?”)

    age = input(“How old are you?”)


    print “Your full name is “, first_name + “ “ + last_name

    Print “And this is your age: “, age

    This will print:

      Your full name is Lando Calrissian

      And this is your age: 50

    More Python Articles
    More By James Payne


       · Hey thanks for dropping by and reading my article on Python. Its a pretty cool...
       · In your demonstration of a while loop, you wrote the following:"Next it creates...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway