Recommended resources
The freely available resources this lesson will direct you to.
- Python Input Function: Visually Explained | #Python Course 6www.youtube.com
- Python Print Function: Visually Explained | #Python Course 4www.youtube.com
- Python Exercises for Beginners: A Hands-On Guide With Answers | DataCampwww.datacamp.com
- Chapter 1 - Python Basics, Automate the Boring Stuff with Python, 3rd Edautomatetheboringstuff.com
Hello! In our last lesson, we established the groundwork by learning how to store information in variables using Python's fundamental data types. This is the first essential skill for any programmer.
Today, we'll build on that by making our programs interactive. Instead of just working with data we define inside the code, we'll learn how to communicate with the userβdisplaying information to them and, crucially, receiving information from them. This lesson focuses on using print() to show output and input() to get user input, which are the foundational tools for building any interactive application, including the chatbots and NLP tools you're interested in.
Displaying Information with print()
You've already seen the print() function in our previous lesson, where we used it to check the values and types of our variables. Now, let's explore it in more detail. The print() function is your primary tool for showing text, results, and messages to the user.
The "Data with Baraa" channel has a great video that breaks down the print() function visually. We'll go through it to understand its core features.
Python Print Function: Visually Explained | #Python Course 4
This video provides a clear, visual explanation of the print() function, from basic usage to more advanced formatting with special characters and multi-line strings.
Let's watch this in a few parts: First, watch the introduction to understand the basic purpose of print(). Next, see how to use single and double quotes to define the string you want to print. Then, watch the segment on special "escape" characters from this section. Pay close attention to \n (new line) and \t (tab), as formatting text output is very important. This also covers how to include quotation marks or backslashes within your text. Finally, learn about using triple quotes for multi-line strings, which is a convenient way to print blocks of text.
To recap the key points from the video:
print()displays the content inside its parentheses on the screen.- You can use single (
') or double (") quotes for your text, but be consistent. - Escape sequences are special characters starting with a backslash (
\) that allow you to format your text.\ncreates a new line.\tinserts a tab.\"or\'lets you include quotation marks inside a string.\\lets you include a literal backslash.
- Triple quotes (
"""..."""or'''...''') allow you to create strings that span multiple lines, preserving the line breaks.
Here are some examples putting this into practice:
# A simple print statement
print("Hello, user!")
# Using a new line character
print("This is line 1.\nThis is line 2.")
# Including quotes in the output
print("She said, \"Python is fun!\"")
# A multi-line string using triple quotes
print("""
User Profile:
- Name: Alex
- Status: Active
""")
Getting User Information with input()
Now for the exciting part: making our programs listen. The input() function pauses your program, prompts the user to type something, and waits for them to press Enter. Whatever the user types is then returned by the function as a string, which you can store in a variable.
This is the core of interactivity. Think about a chatbot asking for your name, a login form asking for a username, or a search engine waiting for your query. They all rely on this fundamental concept.
Let's watch another excellent video from "Data with Baraa" that explains how input() works.
Python Input Function: Visually Explained | #Python Course 6
This video visually explains the input() function, showing how it makes programs interactive and distinguishes between hardcoded and dynamic data.
Please watch the following segments: Start with the purpose of input() from the beginning, which connects it to real-world examples like ChatGPT. Next, watch the visual explanation of the execution flow from this timestamp. See how to store the user's input in a variable in the section from the coding demo. This is a critical step! Finally, understand the difference between hardcoded and dynamic values from this explanation.
As the video demonstrated, the flow is simple: input() displays a prompt, the user types a response, and that response is captured.
Here's a simple example of the code and its execution flow, as shown in the image below.

The code name = input("Enter your name= ") does two things:
- It prints the prompt "Enter your name= " to the screen.
- It waits for the user to type something and press Enter. That text is then assigned to the
namevariable.
A Critical Detail: Handling Input Types
There is one extremely important rule to remember: the input() function always returns a string.
This is true even if the user types in a number. If you ask for an age and the user types 25, the variable will hold the string "25", not the integer 25.
What happens if you try to perform math on a string?
user_age_str = input("What is your age? ")
age_in_a_year = user_age_str + 1 # This will cause an error!
Running this code will result in a TypeError because Python doesn't know how to add an integer (1) to a string ("25").
To fix this, we must convert the string to the correct numeric type using the int() or float() functions we briefly touched on before. The "Automate the Boring Stuff" guide provides an excellent explanation.
Chapter 1 - Python Basics, Automate the Boring Stuff with Python, 3rd Ed
This section of the book explains why you can't mix strings and numbers in operations and demonstrates how to use str(), int(), and float() to convert between types.
In the "Dissecting the Program" section, please read the part titled Type Conversion Functions. Pay close attention to the examples and the diagram showing how Python evaluates an expression involving type conversion.
So, the correct way to handle numeric input is to convert it immediately after receiving it:
user_age_str = input("What is your age? ")
user_age_int = int(user_age_str) # Convert the string to an integer
age_in_a_year = user_age_int + 1
print("Next year, you will be " + str(age_in_a_year) + " years old.")
Notice we also had to use str() to convert the final number back into a string to combine it with the other text for printing. This cycle of converting input to numbers for calculation, and then back to strings for display, is very common.
Practice Time: Your First Interactive Program
Now, let's combine everything you've learned. Your goal is to write a simple program that greets a user personally. This exercise comes from a helpful guide by DataCamp.
Task: The Personalized Greeting
Write a program that does the following:
- Asks the user for their name using
input(). - Asks the user for their age using
input(). - Converts the age from a string to an integer.
- Calculates what the user's age will be in 10 years.
- Prints a personalized message that includes their name and future age.
Here is some starter code from the guide to help you. Try to complete it on your own.
Python Exercises for Beginners: A Hands-On Guide With Answers | DataCamp
This resource provides practical exercises. We'll focus on the first one.
Find the exercise called The personalized greeting. Use the provided starter code to structure your solution. You can check your work against the full solution in the linked workbook if you get stuck, but try to solve it yourself first!
Starter Code:
# 1. Ask for the user's name
name = input("Enter your name: ")
# 2. Ask for the user's age (remember, this is a string)
age_str = input("Enter your age: ")
# 3. Convert the age string to an integer
age_int = int(age_str)
# 4. Calculate their age in 10 years
future_age = age_int + 10
# 5. Print a personalized message
# For example: "Hi [Name], in 10 years you will be [future_age] years old."
print("Hi " + name + ", in 10 years you will be " + str(future_age) + " years old.")
Once you've mastered this, you have the building blocks for the simple chatbot shown in the image below. It's just a series of print and input statements combined with the logic we'll learn next.

Conclusion
In this lesson, you've gained two of the most powerful fundamental tools in programming: print() for communicating to the user and input() for receiving information from them. You also learned the critical step of converting input strings to numbers using int() and float() to perform calculations. These skills are the gateway to creating dynamic, interactive programs.
In our next lesson, we will explore how to make your programs "think." You'll learn to use comparison and logical operators to create conditions, and then use if, elif, and else statements to make your code execute different actions based on the user's input or other conditions. This is where your programs will start to make decisions.