
print()
method
Welcome to our first lesson! In this lesson, we'll dive into Python's print()
method, a cornerstone function used extensively in programming.
The print()
function is a built-in Python feature that instructs the computer to display the values specified within the function.
The print()
function can be used in several ways: without quotes, with single, double, or triple quotes. Let's explore each variation!
Without Quotes
When printing numeric values with the print()
method, the numbers inside the function are not enclosed in quotes:
#print number 520print(520)
#print number 999print(999)
Output:
520
999
Single Quote and Double Quotes
When printing text values (strings), you typically enclose the text in either single quote (') or double quotes ("). This allows Python to recognize the text as a string. Here's an example:
# Printing text values enclosed in single quoteprint('Hello, world!')
# Printing text values enclosed in double quotesprint("Hello, world!")
Output:
Hello, world!
Hello, world!
Why are quotes necessary when printing text but not when printing numeric values in Python?
Python distinguishes between text (strings) and other data types like numbers by using quotes for strings. Strings are sequences of characters enclosed in quotes, helping Python identify them as text. If you use print(Hello, world!)
, Python will report SyntaxError as it doesn't understand texts. See example below.
Numerical values are expressed by their digits and don't require quotes. Python automatically identifies 42 as an integer and 3.14 as a floating-point number without quotes, recognizing them as numeric literals.
#Python will raise SyntaxError if texts are not wrapped by quotesprint(Hello, world!)
Output:
print(Hello, world!) ^SyntaxError: invalid syntax
Triple Quotes
Triple quotes are used in the print()
method if you want to print a multi-line string.
# Using triple quotes in the print method to print a multi-line stringprint("""This is a multi-linestring exampleusing triple quotes.""")
Output:
This is a multi-line
string example
using triple quotes.
Python will raise SyntaxError when using single or double quotes for multi-line strings.
# Using triple quotes in the print method to print a multi-line stringprint("This is a multi-linestring exampleusing triple quotes.")
Output:
print("This is a multi-line ^SyntaxError: EOL while scanning string literal
Q: What would Python return if we type print(1+1)
?
A. 1 + 1
B. 2
Answer:
Click here to view the answer!
The correct answer is B!
This is because 1+1
is a mathematical operation that the computer can directly understand. Therefore, it will directly print the final result of the operation: '2'. This is how the computer demonstrates its ability to 'understand content'
Escape Character
An escape character is a special symbol used to denote a character in a string that cannot be directly included, such as newlines or tabs.
An escape character is a backslash "\" followed by the character you want to insert.
Here are some common escape characters in Python:
- \n: Newline - inserts a newline character.
- \t: Tab - inserts a tab character.
- \': Single Quote - inserts a single quote character.
- \": Double Quote - inserts a double quote character.
- \\: Backslash - inserts a backslash character.
# Using escape characters in a stringprint("This is a newline\nThis is a tab\tThis is a backslash\\")
Output:
This is a newline
This is a tab This is a backslash\
# Using single quote within double quotesprint("He said, 'Hello, world!'")
Output:
He said, 'Hello, world!'
There are 2 ways to include double quotes around "Hello, world!":
# Using single quote within double quotesprint("He said, \"Hello, world!\"")
Output:
He said, "Hello, world!"
# Using double quotes within single quoteprint('He said, "Hello, world!"')
Output:
He said, "Hello, world!"
Let's do a small exercise to wrap up this lesson.
Q: Which of the following option can successfully print out "You did a great job!"
A. print("You did a great job!')
B. print(You did a great job!)
C. print('You did a great job!')
Answer:
Click here to view the answer!
The correct answer is C!
In the upcoming lesson, we'll delve into Python Variables.