In Python, we use the print() function to show messages on the screen. This is useful when giving information to users or checking if our program is working.
A string is text. It must go inside quotation marks: " " or ' '. You've already used strings if youβve ever typed something like print("Hello!").
π‘ Analogy: Think of print() like a loudspeaker for your programβit allows Python to "speak" to the user by displaying messages on the screen.
Use the print() function to display messages
Combine strings using concatenation
Use f-strings to insert variables into text
Forgetting quotation marks: print(Hello) β will cause an error.
Mixing single and double quotes incorrectly: print("It's great!") β vs. print('It's great!') β (apostrophe inside single quotes causes an error).
print() function β A built-in function used to display messages, numbers, or other data on the screen.
Strings β A sequence of characters enclosed in quotation marks (" " or ' '), used to store and display text.
Quotation marks β Symbols used to define strings; either single (') or double (") quotes can be used in Python.
Debugging β The process of identifying and fixing errors in a program by displaying information using print().
Syntax errors β Mistakes in the structure of code that prevent it from running, such as missing quotation marks.
Concatenation β Joining strings together using +
f-string β A special way to mix text and variables using f""
Example 1: Basic Printing
This shows how to print simple text using either double or single quotation marks.
print("Hello, world!")
print('Python is fun!')
Example 2: Concatenation using +
This shows how to join (concatenate) strings and variables together using the + symbol.
name = "Liam"
color = "blue"
print("My name is " + name + " and my favorite color is " + color + ".")
Example 3: f-strings
This shows how to print variables inside a string easily using f-strings, which are clearer and reduce errors.
name = "Liam"
color = "blue"
print(f"My name is {name} and my favorite color is {color}.")
β f-strings are easier to read and less likely to cause errors than using +.
Example 4: Multiline text
This shows how to print text across multiple lines by using triple quotation marks.
# Example 4: Printing multi-line text
print("""Welcome to the Network Setup!
Please choose one of the following options:
1. Create a new star topology
2. Build a bus topology
3. Design a ring topology
Remember: It's important to choose carefully!""")
len(text) β Tells you how many characters are in the string.
β Example: len("apple") β 5
in keyword β Checks if one string is inside another.
β Example: "cat" in "scatter" β True
.lower() β Makes all letters lowercase.
β Example: "Hello".lower() β "hello"
.upper() β Makes all letters uppercase.
β Example: "hi".upper() β "HI"
.strip() β Removes extra spaces at the start and end.
β Example: " wow ".strip() β "wow"
.replace(old, new) β Replaces part of the string.
β Example: "hat".replace("h", "c") β "cat"
.find(text) β Finds the position of a substring. Returns -1 if not found.
β Example: "banana".find("na") β 2
.count(text) β Counts how many times something appears.
β Example: "book".count("o") β 2
.startswith(text) / .endswith(text) β Checks how a string begins or ends.
β Example: "robot".startswith("ro") β True
.split() β Breaks a sentence into words (makes a list).
β Example: "red blue green".split() β ["red", "blue", "green"]
π Practice on W3Schools:
Print Function Tutorial β Learn how to use print() effectively and try different ways to display output.Β
There are additional methods that you can use to control string which you can find out about here.Β
If you ever want to reverse a string, an easy way to do it is described here.
π Try It Online: Run Your Code Here β Test your Python code instantly.
1οΈβ£ Basic Challenge: Print your name and favorite food in two separate lines.
2οΈβ£ Extended Challenge: Print the same info in one line using both:
Concatenation with +
f-string
On a piece of paper, write the example from this lesson.Β
Make sure to include:
β
The print() function
β
Quotation marks around text
β
Different text outputs as shown in the example
Read the example code before writing!