Python Print, Strings and Comments
What you will learn
- How to print several values at once
- How to write strings with quotes and special characters
- How to build text with f-strings
- How to write comments
1. Printing several values
print can take more than one value, separated by commas. It puts a space between them.
print("Hello", "Pathkori")
Output:
Hello Pathkori
You can change the separator and the ending:
print("Hello", "Pathkori", sep="-")
print("a", end="")
print("b")
Output:
Hello-Pathkori
ab
sepis what goes between values (the default is a space).endis what goes after the last value (the default is a new line, so eachprintstarts on a new line).
2. Strings and quotes
A string can use single or double quotes. Use the other kind when the text itself contains a quote:
print('It\'s fine', "He said \"hi\"")
print("It's fine")
print('He said "hi"')
Output:
It's fine He said "hi"
It's fine
He said "hi"
A backslash \ starts a special character. The most useful:
| Code | Meaning |
|---|---|
\n |
new line |
\t |
tab |
\\ |
a backslash |
\' and \" |
a quote mark |
print("Line1\nLine2")
Output:
Line1
Line2
3. Calculations and text together
Numbers and text print differently:
print(2 + 3)
print("2 + 3")
print("Total:", 2 + 3)
Output:
5
2 + 3
Total: 5
Other arithmetic operators:
print(7 / 2, 7 // 2, 7 % 2, 2 ** 5)
Output:
3.5 3 1 32
/divides and gives a decimal: 3.5//is whole-number (floor) division: 3%is the remainder: 1**is a power: 2⁵ = 32
4. f-strings
An f-string lets you put values inside text. Start the string with f and put the value in curly brackets:
name = "Rina"
print(f"Hello, {name}! 2 + 3 = {2 + 3}")
Output:
Hello, Rina! 2 + 3 = 5
name is a variable, a name that holds a value. Chapter 2 covers variables in detail. The curly brackets can hold any calculation.
A related tool: len("Pathkori") gives the number of characters, 8.
5. Comments
A comment is a note for humans. Python ignores everything after a # on that line.
# This whole line is a comment
x = 10 # a comment at the end of a line
print(x)
Output:
10
Good comments explain why you did something, not what the obvious line does. Clear names usually matter more than lots of comments.
Worked example: a small receipt
This program uses variables, f-strings, a calculation, "-" * 20 to draw a line, and the sep and end options of print:
item = "Notebook"
price = 2.5
quantity = 4
print(f"Item: {item}")
print(f"Price: {price}")
print(f"Quantity: {quantity}")
print("-" * 20)
print(f"Total: {price * quantity}")
print("Thank you!", "Come again", sep=" - ", end="!\n")
Output:
Item: Notebook
Price: 2.5
Quantity: 4
--------------------
Total: 10.0
Thank you! - Come again!
| Part | Idea |
|---|---|
f"Item: {item}" |
the value of item is put inside the text; the spaces line the values up |
"-" * 20 |
repeating a string 20 times makes a line |
{price * quantity} |
any calculation can go inside the curly brackets (2.5 times 4 is 10.0) |
sep=" - " |
the two values are joined with - instead of a space |
end="!\n" |
the line ends with ! and a new line |
The item and the numbers are made up.
Common mistakes
- Putting a calculation in quotes and wondering why it prints as text.
- Mixing quote types: starting with
'and ending with". - Forgetting the
fin front of an f-string, which prints{name}literally. - Using
/when you want a whole number. Use//.
Key takeaways
printtakes several values;sepandendchange how they are joined and finished.- Use the other kind of quote inside a string, or a backslash.
\nmakes a new line.- f-strings put values inside text:
f"Hello, {name}". #starts a comment, which Python ignores.
Practice
1. What does print("a", "b", sep="") show?
Show answer
ab. With an empty separator the values run together.
2. What does print(10 // 3, 10 % 3) show?
Show answer
3 1. Ten divided by three is 3 remainder 1.
3. Write a line that prints She said "go" using single quotes around the string.
Show answer
print('She said "go"')
4. With city = "Dhaka", write an f-string that prints I live in Dhaka.
Show answer
print(f"I live in {city}")
5. What does print("Hi\nthere") show?
Show answer
Two lines: Hi and then there.
6. What does print("Thank you!", "Come again", sep=" - ", end="!\n") show?
Show answer
Thank you! - Come again! followed by a new line: sep joins the two values and end replaces the usual new line.