📘 Python Crash Course – Chapter 2: Variables and Simple Data Types – Exercise 2-6 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
📝 Exercise 2-6
Task:Repeat Exercise 2-5, but this time store the famous person’s name in a variable called famous_person . Then compose your message and store it in a new variable called message . Print your message.
✅ Solution Code:
famous_person = "Tony Stark"
message="Learn from youe mistakes"
print(f'{famous_person} said : "{message}"')
🧠 Code Explanation:
This Python code prints a quote by a famous person, with the person's name and the message stored in separate variables. Here's how it works:
-
Store the famous person's name:
The variable
famous_person
is assigned the string"Tony Stark"
, representing the name of the person. -
Store the message:
The variable
message
is assigned the string"Learn from your mistakes"
, which is the quote to be displayed. -
Print the formatted message:
The
print()
function is used to display the quote in a formatted way, combining thefamous_person
andmessage
variables. The output will be:
This code demonstrates how to use variables to store data and format a message using an f-string in Python.
🔍 Output:
Tony Stark said : "Learn from youe mistakes"
📚 Related Exercises from Chapter 2:
- ➤ Exercise 2-1 | Simple Message Part 1
- ➤ Exercise 2-2 | Simple Messages Part 2
- ➤ Exercise 2-3 | Personal Message
- ➤ Exercise 2-4 | Name Cases
- ➤ Exercise 2-5 | Famous Quote
- ➤ Exercise 2-6 | Famous Quote 2
- ➤ Exercise 2-7 | Stripping Names
- ➤ Exercise 2-8 | Number Eight
- ➤ Exercise 2-9 | Favorite Number
- ➤ Exercise 2-10 | Adding Comments
- ➤ Exercise 2-11 | Zen of Python
🔗 Connect With Us:
“The only way to learn a new programming language is by writing programs in it.”
– Dennis Ritchie
📌 Tags: #Python #PythonCrashCourse #CodingForBeginners #LearnPython #PythonProjects #PythonTips