2.3: Python Print Function and String Literals

Print function basics

  • A function is a piece of prewritten code that performs an operation.
  • Python has numerous built-in functions; the most fundamental is the print function, which displays output on the screen.
  • Syntax to call a function: write the function name followed by a set of parentheses, e.g. print(...).
  • When you call the print function, you are invoking or “calling” the function.
  • The argument inside the parentheses is the data you want displayed on the screen.
  • Example: print("Hello, world") displays the text Hello, world.
  • In interactive mode, typing print("Hello, world") and pressing Enter displays: Hello, world (without the quote marks).
  • The statement executes in the order it appears, from top to bottom.

How string literals and quotes work

  • Strings are sequences of characters.
  • A string literal is a string that appears in the code; it must be enclosed in quote marks.
  • In Python, string literals can be enclosed in either single quotes or double quotes:
    • Single quotes: 'Hello'
    • Double quotes: "Hello"
  • The quotes that delimit the string are not displayed in output; they only mark the boundaries of the string.
  • Example: a program that prints a name and address might look like this (the line numbers shown in some books are not part of the program):
    • print("Kate Austin")
    • print("123 Full Circle Drive")
    • print("Asheville, NC 28899")
  • Output for the above program:
    • Kate Austin
    • 123 Full Circle Drive
    • Asheville, NC 28899
  • Important note: statements execute in the order they appear (top to bottom).

Working with quotes inside strings

  • If you want a string literal to contain an apostrophe (right single quote), enclose the string in double quotes:
    • print("don't fear")
    • print("I'm here")
  • If you want a string literal to contain double quotes, enclose the string in single quotes:
    • print('The cat said "meow"')
  • Triple-quoted strings allow both single and double quotes inside and can span multiple lines:
    • Triple-quoted strings can enclose either '''...''' or """...""".
    • They can contain both single and double quotes as part of the string.
    • They can also be used for multiline strings.
  • Example with apostrophes and quotes:
    • print("I'm reading Hamlet tonight") # prints: I'm reading Hamlet tonight
  • Example of a multiline string:
    • print("""One two three""")
    • This prints:
    • One
    • two
    • three

Program example: display name and address

  • The transcript discusses Program two one as an example that outputs a name and address using three print statements.
  • Example code (as described):
    • print("Kate Austin")
    • print("123 Full Circle Drive")
    • print("Asheville, NC 28899")
  • Program output (as described):
    • Kate Austin
    • 123 Full Circle Drive
    • Asheville, NC 28899
  • The line numbers shown in some program listings are only for reference in the book and are not part of the actual program.

Strings vs string literals

  • Strings are data types representing sequences of characters.
  • A string literal is a string value written directly in the code, e.g. 'Hello' or "Hello".
  • In Python, string literals must be enclosed in quotes (single, double, or triple quotes).
  • These literals are interpreted by Python as string values at runtime.

Triple-quoted strings and multiline output

  • Triple quotes allow including both single and double quotes inside the string without escaping.
  • They also enable multiline strings, which is useful for long texts or blocks of text.
  • Examples:
    • print("""I'm reading Hamlet tonight""") # prints I'm reading Hamlet tonight
    • print("""Line one Line two Line three""") # prints three lines

Non-interactive checkpoint questions (practice prompts)

  • 2.7: Write a statement that displays your name.
    • Example answer: print("Your Name")
  • 2.8: Write a statement that displays the following text: Python's the best.
    • Example answer: print("Python's the best.") // using double quotes to include the apostrophe
  • 2.9: Write a statement that displays the following text: The cat said meow.
    • Example answer: print("The cat said meow.")

Recap and key takeaways

  • Print is a built-in function used to display output.
  • The argument to print is the data you want to display.
  • Quotes in the code denote string literals; quotes themselves do not appear in output.
  • You can mix single, double, and triple quotes to manage strings and to include quotes inside strings.
  • Triple-quoted strings support multiline text and contain both single and double quotes without escaping.
  • The order of statements matters; they execute top-to-bottom, and line numbers in listings are instructional, not part of the code.