Basic Input and Output

Input Function:

  • The input function is used to request and get information from the user.
  • The syntax for using the input function is: variable = input('prompt').
  • The prompt is a string, which is a sequence of characters enclosed in quotes.
  • Example: name = input(‘What is your name?: ‘)
  • When the user inputs their name, the input function captures it, and the variable name is assigned this value.
    • What is your name?: Charles

Print Function:

The print function is used to display information on the screen in Python. This may be used to display a message:

>>>print('Welcome to My First Program!')
Welcome to My First Program!

or used to output the value of a variable:

or to display a combination of both strings and variables:

name = input('What is your name?: ')
print('Hello', name)

What is your name?: Charles
Hello Charles

A comma is used to separate the individual items being printed, causing a space to appear between each when displayed.

In Python, input is used to request and get information from the user, and print is used to display information on the screen.

Comments

  • In Python, lines beginning with the hash sign # denote a comment statement.
  • Comment statements are used to provide information for people reading the program.
  • They are ignored during program execution and have no effect on the program results.
# This is a comment statement 
# It provides information about the following code 
name = input('Enter your name: ') # Prompting the user for their name 
print('Hello,', name) # Printing a greeting message with the user's name
Design a site like this with WordPress.com
Get started