Printing ¶
In programming, displaying information is crucial for:
- Debugging: Understanding what's happening in your code.
- User Interaction: Providing feedback or results to users.
- Logging: Recording information for future reference.
In Python, we ask a computer to print a message for us by writing
print()
and putting the message inside the parentheses and enclosed in quotation marks.
Below, we ask the computer to print the message
Hello, world!
.
print("Hello, world!")
Hello, world!
The code is inside the box (known as a code cell), and the computer's response (called the output of the code) is shown below the box. As you can see, the computer printed the message that we wanted.
We can also print multiple things at once and with single,
'
, or double,
"
, quotation marks
print("Alex is", '20', 'years old.')
Alex is 20 years old.
If you want to include quotation marks in the string, you need to "escape" them like so.
print('Alex\'s favorite quote is, "... wait I forgot it".')
Alex's favorite quote is, "... wait I forgot it".
Special characters ¶
When working with text, it’s important to organize it in a way that’s easy to read.
Computers use something called a
newline character
to move text to the next line.
In Python, this newline character is written as
\n
.
Imagine writing a letter or a story where everything is written on one continuous line. It would be hard to read! Instead, you break the text into separate lines for better readability. In the same way, when Python displays text, we often want it to appear on separate lines.
For example:
- Making Output Clearer : If you’re listing items, it’s easier to read them when each item appears on its own line.
- Organizing Long Messages : A single long message looks better when split into smaller pieces.
To add a new line in Python, you can include the newline character,
\n
, inside your text.
print(
"I have a very, very, very,\nvery, very,\nvery long message."
)
I have a very, very, very, very, very, very long message.
The
\n
tells Python to jump to a new line.
You can also add multiple newline characters to create blank lines between text.
print("I have a funny story to tell.\n\n ~ Alex")
I have a funny story to tell. ~ Alex
Note that every individual print statement, by default, will start on a new line.
Here is the same message but with individual
print
statements instead of
\n
.
print("I have a funny story to tell.")
print("")
print(" ~ Alex")
I have a funny story to tell. ~ Alex
However, what if I want Python to print
"Hello\nWorld!"
, but with the actual
\n
and not a new line?
We can do this in one of two ways, and both only require typing one extra character. First, we can tell Python to "escape" the special character.
print("Hello\\nWorld!")
Hello\nWorld!
The other way is to tell Python to ignore all special characters and treat it as a raw string.
In Python, a raw string tells the program to treat the string exactly as it is written, without interpreting special characters like
\n
.
This can be helpful when you want to include special characters in your string without having to escape them.
To create a raw string, prefix the string with an
r
or
R
.
When Python sees this prefix, it ignores the special meaning of escape sequences and interprets the backslashes as literal characters.
print(r"Hello\nWorld!\\")
Hello\nWorld!\\
Multiline strings ¶
Sometimes, you may need to display a long message or text that spans multiple lines.
Instead of using multiple
print()
statements or adding a
\n
for every new line, Python provides a simpler way to handle this:
multiline strings
.
A multiline string allows you to write text that spans several lines without having to use
\n
explicitly. In Python, you create a multiline string by enclosing the text in
triple quotes
(
'''
or
"""
).
print("""This is a multiline string.
It allows you to write text
on multiple lines without needing \\n.""")
This is a multiline string. It allows you to write text on multiple lines without needing \n.
Notice how the text appears exactly as it was written in the code, with each line starting on a new line.
You can also leave blank lines within a multiline string to add extra space:
print('''This is the first line.
This is the third line.''')
This is the first line. This is the third line.