Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Engineering LibreTexts

1.2: Input/Output

( \newcommand{\kernel}{\mathrm{null}\,}\)

Learning Objectives

By the end of this section you should be able to

  • Display output using the print() function.
  • Obtain user input using the input() function.

Basic output

The print() function displays output to the user. Output is the information or result produced by a program. The sep and end options can be used to customize the output. Table 1.1 shows examples of sep and end.

Multiple values, separated by commas, can be printed in the same statement. By default, each value is separated by a space character in the output. The sep option can be used to change this behavior.

By default, the print() function adds a newline character at the end of the output. A newline character tells the display to move to the next line. The end option can be used to continue printing on the same line.

Code Output

print("Today is Monday.")
print("I like string beans.")

Today is Monday.
I like string beans.

print("Today", "is", "Monday")
print("Today", "is", "Monday", sep="...")

Today is Monday
Today...is...Monday

print("Today is Monday, ", end="")
print("I like string beans.")

Today is Monday, I like string beans.

print("Today", "is", "Monday", sep="? ", end="!!")
print("I like string beans.")

Today? is? Monday!!I like string beans.

Table 1.1 Example uses of
print()
.
Checkpoint: Displaying output to the user
Concepts in Practice: The print() function
1.
Which line of code prints Hello world! as one line of output?
  1. print(Hello world!)
  • print("Hello", "world", "!")
  • print("Hello world!")
  • 2.
    Which lines of code prints Hello world! as one line of output?
    1. print("Hello")
      print(" world!")
      
    2. print("Hello")
      print(" world!", end="")
      
    3. print("Hello", end="")
      print(" world!")
      
    3.
    What output is produced by the following statement?
    print("555", "0123", sep="-")
    1. 555 0123
    2. 5550123-
    3. 555-0123
    Do spaces really matter?

    Spaces and newline characters are not inherently important. However, learning to be precise is an essential skill for programming. Noticing little details, like how words are separated and how lines end, helps new programmers become better.

    Basic input

    Computer programs often receive input from the user. Input is what a user enters into a program. An input statement, variable = input("prompt"), has three parts:

    1. A variable refers to a value stored in memory. In the statement above, variable can be replaced with any name the programmer chooses.
    2. The input() function reads one line of input from the user. A function is a named, reusable block of code that performs a task when called. The input is stored in the computer's memory and can be accessed later using the variable.
    3. A prompt is a short message that indicates the program is waiting for input. In the statement above, "prompt" can be omitted or replaced with any message.
    Checkpoint: Obtaining input from the user
    Concepts in Practice: The input() function
    4.
    Which line of code correctly obtains and stores user input?
    1. input()
  • today_is = input
  • today_is = input()
  • 5.
    Someone named Sophia enters their name when prompted with
    print("Please enter your name: ")
    name = input()
    
    What is displayed by print("You entered:", name)?
    1. You entered: name
    2. You entered: Sophia
    3. You entered:, Sophia
    6.
    What is the output if the user enters "six" as the input?
    print("Please enter a number: ")
    number = input()
    print("Value =", number)
    
    1. Value = six
    2. Value = 6
    3. Value = number
    Try It: Frost poem

    Write a program that uses multiple print() statements to output the following poem by Robert Frost. Each print() statement should correspond to one line of output.

    Tip: You don't need to write the entire program all at once. Try writing the first print() statement, and then click the Run button. Then write the next print() statement, and click the Run button again. Continue writing and testing the code incrementally until you finish the program.

        I shall be telling this with a sigh
        Somewhere ages and ages hence:
        Two roads diverged in a wood, and I--
        I took the one less traveled by,
        And that has made all the difference.
    Try It: Name and likes

    Write a program that asks the following two questions (example input in bold):

        What is your name? Shakira
        What do you like? singing

    Output a blank line after reading the input. Then output the following message based on the input:

        Shakira likes singing

    This page titled 1.2: Input/Output is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.

    Support Center

    How can we help?