How does mpg turn to str?


#1

Continuing the discussion from 7. Creating class methods:




https://www.codecademy.com/en/courses/python-intermediate-en-egNXj/1/1?curriculum_id=4f89dab3d788890003000096#


I just want to know How dose this code turn the pmg into the str without using str()?
Thanks for helping. :relaxed:

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    def display_car (self):
        return ( "This is a %s %s with %s MPG."%(self.color, self.model, self.mpg))
    
my_car = Car("DeLorean", "silver", 88)

print my_car.display_car()


#2

When you use %s it tells python that you want to format the object like a string. So mpg is being stored as an int up until it gets passed to the %s where it becomes formatted as a sting.


#3

Thank you, I got it. :smiley:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.