Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

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!)
  2. print("Hello", "world", "!")
  3. 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()
  2. today_is = input
  3. 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
Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Jul 30, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.