🐾 Python Classes and f-Strings: A Beginner’s Blog-Lesson
“Meowww!” says Luna, and just like that, your journey into Python’s object-oriented programming begins…
🏗️ Step 1: Creating the Cat
Class
Let’s say you want to create a cat in your program. Not just any cat—a cat with a name, an age, and the ability to say meow. You do this using a Python class.
Here’s the basic code:
class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
def meow(self):
print(f"{self.name} says Meowww! 🐾")
Let’s break it down:
class Cat:
— This defines a new class namedCat
.__init__
— This is the constructor method, which is automatically called when you create a newCat
object.self.name
andself.age
— These are attributes that store the cat’s name and age.meow()
— This method prints a message that includes the cat’s name.
🐱 Step 2: Creating and Using the Cat Object
Once you have the class, you can create a cat:
my_cat = Cat("Luna", 3)
my_cat.meow()
When you run this code, Luna introduces herself:
Luna says Meowww! 🐾
🧠 Step 3: Understanding the Mysterious f
in f"{self.name}"
Now here comes the magic part: What does that f
before the string mean?
print(f"{self.name} says Meowww! 🐾")
That f
stands for formatted string literal, or simply f-string.
It allows you to insert variables directly into a string using curly braces {}
.
🎉 Example:
name = "Whiskers"
age = 2
print(f"{name} is {age} years old.")
Output:
Whiskers is 2 years old.
You can even do math inside the curly braces!
print(f"{name} will be {age + 1} next year.")
Output:
Whiskers will be 3 next year.
🧪 Step 4: Add More Personality to Your Cat
Want your cat to do more? Try this:
class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
def meow(self):
print(f"{self.name} says Meowww! 🐾")
def describe(self):
print(f"My name is {self.name} and I am {self.age} years old.")
Now you can call:
my_cat.describe()
Output:
My name is Luna and I am 3 years old.
📝 Practice and Review Questions
- What does the
__init__
method do in a Python class? - Rewrite the
Cat
class to include apurr()
method that prints a purring sound. - What does the
f
inf-string
stand for, and what is it used for? - How do you create and use a new
Cat
object named “Oreo” who is 4 years old? - How would you change the
meow()
method so that it prints louder meows for older cats (age > 5)?