Lesson 5: Input and Output
Make your Python programs interactive by learning how to accept user input and format output.
Lesson 5: Input and Output
So far, our programs have been "monologues." Python talks to us, but we don't talk back. In this lesson, we will learn how to turn our scripts into Dialogues using the input() function and how to format our output beautifully using f-strings.
Lesson Overview
In this lesson, we will:
- The
input()Function: Getting data from the keyboard. - Type Casting: The secret trick to turning text into numbers.
- f-strings: The professional way to format output.
- Bringing it all together: Building a basic interactive script.
1. Getting Input from the User
The input() function pauses the program and waits for the user to type something. Whatever they type is stored as a String.
# The text inside the quotes is called the 'prompt'
name = input("Enter your name: ")
print(name)
Wait! Even if you type a number (like 25), Python treats it as text ("25"). To do math with it, we need our next tool.
2. Type Casting (Converting Data)
Let's say we want to ask for a user's age and add 1 to it. If we don't "cast" it, the program will crash!
# WRONG WAY
age = input("Enter your age: ")
# print(age + 1) # This would cause an ERROR
To fix this, we wrap the input() function in a casting function:
int(): Convert to a whole number.float(): Convert to a decimal.str(): Convert to text.
# RIGHT WAY
age = int(input("Enter your age: "))
next_year = age + 1
print(next_year)
3. Formatting Output with f-strings
When you want to mix variables and text in a print() statement, the best way is to use an f-string (the "f" stands for formatted).
Just put an f before your quotes, and put your variables inside curly braces {}.
name = "Alex"
score = 95
# The old way (clunky):
# print("Hello " + name + ", your score is " + str(score))
# The f-string way (pro):
print(f"Hello {name}, your score is {score}!")
4. Multi-line Output
Sometimes you want to print a long block of text. You can use triple quotes """ for this.
print("""
Welcome to the Arena!
1. Start Game
2. Options
3. Exit
""")
Practice Exercise: The Greeter
Create a file named greeter.py. Your program should:
- Ask the user for their first name.
- Ask the user for their favorite number.
- Calculate what that number would be if it were multiplied by 2.
- Print a message like:
"Hi [Name]! Did you know that [Number] x 2 is [Result]?"
Bonus: Use f-strings for the final message!
Quick Knowledge Check
- What data type does the
input()function always return by default? - What function do you use to convert a string into an integer?
- How do you signify that a string is an f-string?
- Why is type casting important when taking numerical input?
Key Takeaways
input()takes data from the user and returns a string.- Use
int()orfloat()to convert numerical input for math. - f-strings make combining variables and text easy and readable.
- The text inside
input()is the prompt that tells the user what to do.
What’s Next?
Now that our programs can handle user data, let's learn how to do complex math and logic with it! In Lesson 6, we’ll explore Operators and Expressions.