Skip to main content
Engineering LibreTexts

1.2: Input/Output

  • Page ID
    117514
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

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

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

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

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)
    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.

    Table 1.1 Example uses of print().
    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.

    Checkpoint: Displaying output to the user
    Concepts in Practice \(\PageIndex{1}\): The print() function

    Which line of code prints Hello world! as one line of output?

    1. print(Hello world!)
    2. print("Hello", "world", "!")
    3. print("Hello world!")
    Answer

    c. The hello world program is just one line of code.

    Concepts in Practice \(\PageIndex{2}\): The print() function

    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!")
    Answer

    c. The end="" option removes the newline character after hello, producing Hello world! on the same line.

    Concepts in Practice \(\PageIndex{3}\): The print() function

    What output is produced by the following statement?

    print("555", "0123", sep="-")
    1. 555 0123
    2. 5550123-
    3. 555-0123
    Answer

    c. The default space character that separates multiple items has been replaced with the dash character.

    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 \(\PageIndex{4}\): The input() function

    Which line of code correctly obtains and stores user input?

    1. input()
    2. today_is = input
    3. today_is = input()
    Answer

    c. The user input is stored in memory and can be referenced using today_is.

    Concepts in Practice \(\PageIndex{5}\): The input() function

    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
    Answer

    b. The print() function outputs "You entered:" followed by a space character, followed by the input data, Sophia, which is referenced by the variable name.

    Concepts in Practice \(\PageIndex{6}\): The input() function

    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
    Answer

    a. The input is stored exactly as the user enters the input. So, the retrieved value is displayed exactly as entered by the user.

    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.
    
    Interactive Code
    # Type your code after this comment
     
    Answer

    print("I shall be telling this with a sigh")
    print("Somewhere ages and ages hence:")
    print("Two roads diverged in a wood, and I--")
    print("I took the one less traveled by,")
    print("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
    
    Interactive Code
    # Type your code after this comment
     
    Answer

    print("What is your name? ")
    name = input()
    print("What do you like? ")
    like = input()
    print()
    print(name, "likes", like)


    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.